<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="php,">










<meta name="description" content="S.O.I.L.D 之单一职责12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697">
<meta name="keywords" content="php">
<meta property="og:type" content="article">
<meta property="og:title" content="php 代码段">
<meta property="og:url" content="http://yoursite.com/2019/07/16/php-代码段/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="S.O.I.L.D 之单一职责12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-11T09:26:57.547Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="php 代码段">
<meta name="twitter:description" content="S.O.I.L.D 之单一职责12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/07/16/php-代码段/">



<meta name="referrer" content="never"> ​​​​


  <title>php 代码段 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/07/16/php-代码段/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">php 代码段</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-16T13:43:21+08:00">
                2019-07-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  17.3k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  88 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="S-O-I-L-D-之单一职责"><a href="#S-O-I-L-D-之单一职责" class="headerlink" title="S.O.I.L.D 之单一职责"></a>S.O.I.L.D 之单一职责</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Acme\Reporting;</span><br><span class="line"></span><br><span class="line">use Auth;</span><br><span class="line">use DB;</span><br><span class="line">use Exception;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalesReporter</span></span></span><br><span class="line"><span class="class"></span>&#123;   </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取某个时间段的销售总额</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $startDate</span></span><br><span class="line"><span class="comment">     * @param  $endDate  </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate</span>) : <span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (! Auth::check()) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">'Authentication required for reporting'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;queryDBForSalesBetween($startDate, $endDate);</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;format($sales);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 查询销售报表数据</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $startDate </span></span><br><span class="line"><span class="comment">     * @param  $endDate  </span></span><br><span class="line"><span class="comment">     *  </span></span><br><span class="line"><span class="comment">     * @return float         </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">queryDBForSalesBetween</span>(<span class="params">$startDate, $endDate</span>) : <span class="title">float</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DB::table(<span class="string">'sales'</span>)-&gt;whereBetween(<span class="string">'created_at'</span>, [$startDate, $endDate])-&gt;sum(<span class="string">'charge'</span>) / <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 数据展示</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $sales</span></span><br><span class="line"><span class="comment">     * @return string     </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">format</span>(<span class="params">$sales</span>) : <span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"&lt;h1&gt;Sales: $sales&lt;/h1&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">测试</span><br><span class="line"></span><br><span class="line">$report = <span class="keyword">new</span> Acme\Reporting\SalesReporter();</span><br><span class="line">$report-&gt;between(</span><br><span class="line">    now()-&gt;subDays(<span class="number">10</span>), </span><br><span class="line">    now()</span><br><span class="line">);</span><br><span class="line">该例子明显违反了单一职责:</span><br><span class="line"></span><br><span class="line">授权方式发生变化时，如 API 授权，需要改动该类</span><br><span class="line">当数据库层发生变化时候，如使用 Redis 时，需要改动该类</span><br><span class="line">当展示方式发生变化时，需要改动该类</span><br><span class="line">正面示例</span><br><span class="line"></span><br><span class="line">对于上述的例子，应当作出如下的改动：</span><br><span class="line"></span><br><span class="line">不需要关心用户授权，用户授权与本类的职责无关</span><br><span class="line">数据层应当剥离出来</span><br><span class="line">展示层应当剥离出来</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="comment">// 展示层接口</span></span><br><span class="line">interface SalesOutputInterface &#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">output</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">// 展示层实现</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">HtmlOutput</span> <span class="title">implements</span> <span class="title">SalesOutputInterface</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">output</span>(<span class="params">$sales</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;h1&gt;&#123;$sales&#125;&lt;/h1&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数据层</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalesRepository</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DB::table(<span class="string">'sales'</span>)-&gt;whereBetween(<span class="string">'create_at'</span>, [$startDate, $endDate])-&gt;sum(<span class="string">'charge'</span>) / <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 职责类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalsReporter</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public $repo;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$repo</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;repo = $repo;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate, SalesOutputInterface $formater</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;repo-&gt;between($startDate, $endDate);</span><br><span class="line">        $formater-&gt;output($sales);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">测试</span><br><span class="line"></span><br><span class="line">$report = <span class="keyword">new</span> SalsReporter(<span class="keyword">new</span> SalesRepository);</span><br><span class="line">$report-&gt;between(</span><br><span class="line">    now-&gt;subDays(<span class="number">10</span>), </span><br><span class="line">    now(),</span><br><span class="line">    <span class="keyword">new</span> HtmlOutput</span><br><span class="line">);</span><br><span class="line">结合 Laravel 的依赖注入，可以进一步简化https:<span class="comment">//learnku.com/articles/27923#topnav</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalsReporter</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public $repo;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">SalesRepository $repo</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;repo = $repo;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate, SalesOutputInterface $formater</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;repo-&gt;between($startDate, $endDate);</span><br><span class="line">        $formater-&gt;output($sales);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字节转换"><a href="#字节转换" class="headerlink" title="字节转换"></a>字节转换</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 转化内存信息https://github.com/yiranzai/php-tools</span></span><br><span class="line"><span class="comment">     * @param      $bytes</span></span><br><span class="line"><span class="comment">     * @param bool $binaryPrefix</span></span><br><span class="line"><span class="comment">     * @return string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getNiceFileSize</span>(<span class="params">int $bytes, bool $binaryPrefix = true</span>): <span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($binaryPrefix) &#123;</span><br><span class="line">            $unit = array(<span class="string">'B'</span>, <span class="string">'KiB'</span>, <span class="string">'MiB'</span>, <span class="string">'GiB'</span>, <span class="string">'TiB'</span>, <span class="string">'PiB'</span>);</span><br><span class="line">            <span class="keyword">if</span> ($bytes === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">'0 '</span> . $unit[<span class="number">0</span>];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> @round($bytes / (<span class="number">1024</span> ** ($i = floor(log($bytes, <span class="number">1024</span>)))), <span class="number">2</span>) . <span class="string">' '</span> . ($unit[(int)$i] ?? <span class="string">'B'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $unit = array(<span class="string">'B'</span>, <span class="string">'KB'</span>, <span class="string">'MB'</span>, <span class="string">'GB'</span>, <span class="string">'TB'</span>, <span class="string">'PB'</span>);</span><br><span class="line">        <span class="keyword">if</span> ($bytes === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'0 '</span> . $unit[<span class="number">0</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> @round($bytes / (<span class="number">1000</span> ** ($i = floor(log($bytes, <span class="number">1000</span>)))), <span class="number">2</span>) . <span class="string">' '</span> . ($unit[(int)$i] ?? <span class="string">'B'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 四舍五入 格式化除法</span></span><br><span class="line"><span class="comment">         * @param     $divisor</span></span><br><span class="line"><span class="comment">         * @param     $divided</span></span><br><span class="line"><span class="comment">         * @param int $scale</span></span><br><span class="line"><span class="comment">         * @return int|string</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">formatDiv</span>(<span class="params">$divisor, $divided, int $scale = <span class="number">2</span></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (empty((int)$divided)) &#123;</span><br><span class="line">                <span class="keyword">return</span> sprintf(<span class="string">'%.'</span> . $scale . <span class="string">'f'</span>, <span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> $scale === <span class="number">0</span> ?</span><br><span class="line">                bcdiv($divisor, $divided) :</span><br><span class="line">                sprintf(<span class="string">'%.'</span> . $scale . <span class="string">'f'</span>, bcdiv($divisor, $divided, $scale + <span class="number">1</span>));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 求两个数的最大公约数</span></span><br><span class="line"><span class="comment">             *</span></span><br><span class="line"><span class="comment">             * @param $a</span></span><br><span class="line"><span class="comment">             * @param $b</span></span><br><span class="line"><span class="comment">             * @return int</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">gcd</span>(<span class="params">int $a, int $b</span>): <span class="title">int</span></span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="keyword">if</span> ($a === <span class="number">0</span> || $b === <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">return</span> abs(max(abs($a), abs($b)));</span><br><span class="line">                &#125;</span><br><span class="line">        </span><br><span class="line">                $r = $a % $b;</span><br><span class="line">                <span class="keyword">return</span> ($r !== <span class="number">0</span>) ?</span><br><span class="line">                    self::gcd($b, $r) :</span><br><span class="line">                    abs($b);</span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 求一个数组的最大公约数</span></span><br><span class="line"><span class="comment">             *</span></span><br><span class="line"><span class="comment">             * @param array $array</span></span><br><span class="line"><span class="comment">             * @param int   $a</span></span><br><span class="line"><span class="comment">             * @return int</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">gcdArray</span>(<span class="params">array $array, $a = <span class="number">0</span></span>): <span class="title">int</span></span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                $b = array_pop($array);</span><br><span class="line">                <span class="keyword">return</span> ($b === <span class="literal">null</span>) ?</span><br><span class="line">                    (int)$a :</span><br><span class="line">                    self::gcdArray($array, <span class="attr">self</span>::gcd($a, $b));</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">                 * 每次都取中间的那个数，遍历数组，比它大放右边，比它小放左边  快速排序</span></span><br><span class="line"><span class="comment">                 *</span></span><br><span class="line"><span class="comment">                 * @param array $array</span></span><br><span class="line"><span class="comment">                 * @return array</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">quickSort</span>(<span class="params">array $array</span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">                </span>&#123;</span><br><span class="line">                    $len = count($array);</span><br><span class="line">                    <span class="keyword">if</span> ($len &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">                        <span class="keyword">return</span> $array;</span><br><span class="line">                    &#125;</span><br><span class="line">                    $m      = $len &gt;&gt; <span class="number">1</span>;<span class="comment">//取中间值 50&gt;&gt;1 25 51&gt;&gt;1 25 52&gt;&gt;1 26</span></span><br><span class="line">                    $mValue = $array[$m];</span><br><span class="line">                    $left   = $right = [];</span><br><span class="line">                    foreach ($array <span class="keyword">as</span> $key =&gt; $iValue) &#123;</span><br><span class="line">                        <span class="keyword">if</span> ($key === $m) &#123;</span><br><span class="line">                            <span class="keyword">continue</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                        <span class="keyword">if</span> ($iValue &lt; $mValue) &#123;</span><br><span class="line">                            $left[] = $iValue;</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            $right[] = $iValue;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">return</span> array_merge(self::quickSort($left), [$mValue], <span class="attr">self</span>::quickSort($right));</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">quick</span>(<span class="params">$arr</span>)</span>&#123;</span><br><span class="line">                        <span class="keyword">if</span>(count($arr)&lt;=<span class="number">1</span>)&#123; <span class="comment">//如果数组根本就一个元素就直接返回 不用在排序咯</span></span><br><span class="line">                            <span class="keyword">return</span> $arr;</span><br><span class="line">                        &#125;</span><br><span class="line">                </span><br><span class="line">                        $k=$arr[<span class="number">0</span>];<span class="comment">//定义一个初始要排序的值 默认为数组第一个</span></span><br><span class="line">                        $x=array();<span class="comment">//定义比要排序的值 小的数组块</span></span><br><span class="line">                        $y=array();<span class="comment">//定义比要排序的值 大的数组块</span></span><br><span class="line">                        $_size=count($arr);<span class="comment">//统计数组的大小</span></span><br><span class="line">                        <span class="keyword">for</span>($i=<span class="number">1</span>;$i&lt;$_size;$i++)&#123;<span class="comment">//循环数组 记住这边要从索引1 开始</span></span><br><span class="line">                            <span class="keyword">if</span>($arr[$i]&lt;=$k)&#123;<span class="comment">//如果当前的值小于 要排序的值</span></span><br><span class="line">                                $x[]=$arr[$i];<span class="comment">//就把小于的值放到 小的数组块中</span></span><br><span class="line">                            &#125;elseif($arr[$i]&gt;$k)&#123;<span class="comment">//如果当前的值大于 要排序的值</span></span><br><span class="line">                                $y[]=$arr[$i];<span class="comment">//就把大于的值放到 大的数组块中</span></span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                        $x=Sort::quick($x);<span class="comment">//依次递归执行 这样就会得到小的数组块</span></span><br><span class="line">                        $y=Sort::quick($y);<span class="comment">//依次递归执行 这样就会得到大的数组块</span></span><br><span class="line">                        <span class="keyword">return</span> array_merge($x,array($k),$y);<span class="comment">//最后在合并下 小的模块+中间的模块【初始要排序的值】+大的模块 就ok~</span></span><br><span class="line">                    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="php多线程"><a href="#php多线程" class="headerlink" title="php多线程"></a>php多线程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 继承 Thread 的类具有创建线程的能力</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Request</span> <span class="keyword">extends</span> <span class="title">Thread</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $sql;</span><br><span class="line"></span><br><span class="line">    private $dsn;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$sql, $dsn</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;sql = $sql;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;dsn = $dsn;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">run</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $db = <span class="keyword">new</span> PDO($<span class="keyword">this</span>-&gt;dsn);</span><br><span class="line"></span><br><span class="line">        $stat1 = $db-&gt;query($<span class="keyword">this</span>-&gt;sql);</span><br><span class="line"></span><br><span class="line">        $result = $stat1-&gt;fetchAll(PDO::FETCH_ASSOC);</span><br><span class="line"></span><br><span class="line">        print_r($result);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$dsn = <span class="string">'sqlite:/tmp/pselect.db'</span>;</span><br><span class="line">$db = <span class="keyword">new</span> PDO($dsn);</span><br><span class="line"></span><br><span class="line">$db-&gt;exec(<span class="string">'create table users(id int, name varchar(255))'</span>);</span><br><span class="line">$db-&gt;exec(<span class="string">'create table books(id int, name varchar(255))'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//$db-&gt;exec("insert into users(id, name) values(1, '张三')");</span></span><br><span class="line"><span class="comment">//$db-&gt;exec("insert into users(id, name) values(2, '李四')");</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//$db-&gt;exec("insert into books(id, name) values(1, '三国')");</span></span><br><span class="line"><span class="comment">//$db-&gt;exec("insert into books(id, name) values(2, '水浒')");</span></span><br><span class="line"></span><br><span class="line">$sql = [</span><br><span class="line">    <span class="string">'select * from users'</span>,</span><br><span class="line">    <span class="string">'select * from books'</span>,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="comment">//$stat1 = $db-&gt;query($sql[0]);</span></span><br><span class="line"><span class="comment">//$stat2 = $db-&gt;query($sql[1]);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//$results1 = $stat1-&gt;fetchAll(PDO::FETCH_ASSOC);</span></span><br><span class="line"><span class="comment">//$results2 = $stat2-&gt;fetchAll(PDO::FETCH_ASSOC);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//print_r($results1);</span></span><br><span class="line"><span class="comment">//print_r($results2);</span></span><br><span class="line"></span><br><span class="line">$arr = [];</span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">2</span>; $i++) &#123;</span><br><span class="line">    $request = <span class="keyword">new</span> Request($sql[$i], $dsn);</span><br><span class="line">    $arr[$i] = $request;</span><br><span class="line">    <span class="comment">// 创建新线程，随后线程会执行 run 方法</span></span><br><span class="line">    <span class="keyword">if</span> (! $request-&gt;start()) &#123;</span><br><span class="line">        die(<span class="string">"Start thread failed\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    echo <span class="string">"Thread(&#123;$i&#125;) started\n"</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">2</span>; $i++) &#123;</span><br><span class="line">    <span class="comment">// join 是阻塞的，所以脚本运行时间取决于耗时最长的线程</span></span><br><span class="line">    <span class="keyword">if</span> (! $arr[$i]-&gt;join()) &#123;</span><br><span class="line">        die(<span class="string">"Join failed\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/26812</span></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">select</span>(<span class="params">$array</span>)</span>&#123;</span><br><span class="line">        $count=count($array);<span class="comment">//取出数组的总长度</span></span><br><span class="line">        <span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;$count<span class="number">-1</span>;$i++)&#123;<span class="comment">//外部循环 依次循环</span></span><br><span class="line">            <span class="comment">/* 找出最小的元素 开始*/</span></span><br><span class="line">            $min=$i;<span class="comment">//查找到最低的元素</span></span><br><span class="line">            <span class="keyword">for</span>($j=$i+<span class="number">1</span>;$j&lt;$count;$j++)&#123;</span><br><span class="line">                <span class="comment">//由小到大排列</span></span><br><span class="line">                <span class="keyword">if</span>($array[$min]&gt;$array[$j])&#123;<span class="comment">//如果当前的元素比后面的元素大</span></span><br><span class="line">                    $min=$j;<span class="comment">//就把最小的元素指针替换成后面元素</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">/* 找出最小的元素 结束*/</span>选择排序</span><br><span class="line"></span><br><span class="line">            <span class="comment">/*swap$array[$i]and$array[$min]即将当前内循环的最小元素放在$i位置上*/</span></span><br><span class="line">            <span class="keyword">if</span>($min!=$i)&#123;<span class="comment">//如果找出的最小的元素不在当前的循环的索引位置</span></span><br><span class="line">                $temp=$array[$min];<span class="comment">//当前元素存储临时变量</span></span><br><span class="line">                $array[$min]=$array[$i];<span class="comment">//把最小位置上的元素替换成当前的元素</span></span><br><span class="line">                $array[$i]=$temp;<span class="comment">//把当前的元素替换成最小的元素</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $array;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">//模式分隔符后的"i"标记这是一个大小写不敏感的搜索</span></span><br><span class="line"><span class="keyword">if</span> (preg_match(<span class="string">"/php/i"</span>, <span class="string">"PHP is the web scripting language of choice."</span>)) &#123;</span><br><span class="line">    echo <span class="string">"查找到匹配的字符串 php。"</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    echo <span class="string">"未发现匹配的字符串 php。"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="大文件传输解决方案"><a href="#大文件传输解决方案" class="headerlink" title="大文件传输解决方案"></a>大文件传输解决方案</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">sliceDownload</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        $path = <span class="string">'slice/'</span>.date(<span class="string">'Ymd'</span>)  ;</span><br><span class="line"></span><br><span class="line">        $filename = $path .<span class="string">'/'</span>. <span class="string">'周杰伦 - 黑色幽默 [mqms2].mp3'</span> ;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取文件资源https://learnku.com/articles/31108</span></span><br><span class="line">        $file = Storage::disk(<span class="string">'admin'</span>)-&gt;readStream($filename);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取文件大小</span></span><br><span class="line">        $fileSize = Storage::disk(<span class="string">'admin'</span>)-&gt;size($filename);</span><br><span class="line"></span><br><span class="line">        header(<span class="string">"Content-type:application/octet-stream"</span>);<span class="comment">//设定header头为下载</span></span><br><span class="line">        header(<span class="string">"Accept-Ranges:bytes"</span>);</span><br><span class="line">        header(<span class="string">"Accept-Length:"</span>.$fileSize);<span class="comment">//响应大小</span></span><br><span class="line">        header(<span class="string">"Content-Disposition: attachment; filename=周杰伦 - 黑色幽默 [mqms2].mp3"</span>);<span class="comment">//文件名</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//不设置的话要等缓冲区满之后才会响应</span></span><br><span class="line">        ob_end_clean();<span class="comment">//缓冲区结束</span></span><br><span class="line">        ob_implicit_flush();<span class="comment">//强制每当有输出的时候,即刻把输出发送到浏览器\</span></span><br><span class="line">        header(<span class="string">'X-Accel-Buffering: no'</span>); <span class="comment">// 不缓冲数据</span></span><br><span class="line"></span><br><span class="line">        $limit=<span class="number">1024</span>*<span class="number">1024</span>;</span><br><span class="line">        $count=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//限制每秒的速率</span></span><br><span class="line">        <span class="keyword">while</span>($fileSize-$count&gt;<span class="number">0</span>)&#123;<span class="comment">//循环读取文件数据</span></span><br><span class="line">            $data=fread($file,$limit);</span><br><span class="line">            $count+=$limit;</span><br><span class="line">            echo $data;<span class="comment">//输出文件</span></span><br><span class="line">            sleep(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP-生成奖状"><a href="#PHP-生成奖状" class="headerlink" title="PHP 生成奖状"></a>PHP 生成奖状</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">ob_clean();</span><br><span class="line">$realname = <span class="string">"姓名:最闲的码农"</span>;</span><br><span class="line">$schoolname = <span class="string">"社区:Laravel"</span>; </span><br><span class="line"></span><br><span class="line">$image = imagecreatefrompng(<span class="string">'1562926506930.png'</span>);      <span class="comment">//书模版图片文件的路径 必须是png格式的文件</span></span><br><span class="line">$red = imagecolorallocate($image,<span class="number">00</span>,<span class="number">00</span>,<span class="number">00</span>); <span class="comment">// 字体颜色</span></span><br><span class="line"><span class="comment">// imageTTFText("Image", "Font Size", "Rotate Text", "Left Position","Top Position", "Font Color", "Font Name", "Text To Print");</span></span><br><span class="line"><span class="comment">//根据文本填写的位置不同https://learnku.com/articles/31120</span></span><br><span class="line"><span class="comment">//"Left Position 和 Top Position 可以使用 getimagesize进行配合计算文字x和y轴坐标</span></span><br><span class="line">imageTTFText($image, <span class="number">50</span>, <span class="number">0</span>, <span class="number">628</span>, <span class="number">615</span>, $red, <span class="string">'simheittf.ttf'</span>,$realname);</span><br><span class="line">imageTTFText($image, <span class="number">50</span>, <span class="number">0</span>, <span class="number">628</span>, <span class="number">714</span>, $red, <span class="string">'simheittf.ttf'</span>, $schoolname);</span><br><span class="line">header(<span class="string">'Content-type: image/png;'</span>);</span><br><span class="line">ImagePng($image);</span><br><span class="line">imagedestroy($image);</span><br><span class="line">$filename = <span class="string">'certificate_aadarsh.png'</span>;</span><br><span class="line">ImagePng($image, $filename);</span><br><span class="line">imagedestroy($image);</span><br></pre></td></tr></table></figure>
<h3 id="Composer-install-报错"><a href="#Composer-install-报错" class="headerlink" title="Composer install 报错"></a>Composer install 报错</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Finished: success: <span class="number">62</span>, <span class="attr">skipped</span>: <span class="number">0</span>, <span class="attr">failure</span>: <span class="number">0</span>, <span class="attr">total</span>: <span class="number">62</span></span><br><span class="line">Package operations: <span class="number">62</span> installs, <span class="number">0</span> updates, <span class="number">0</span> removals</span><br><span class="line">  - Installing kylekatarnls/update-helper (<span class="number">1.1</span><span class="number">.1</span>): Loading <span class="keyword">from</span> cache</span><br><span class="line">Plugin installation failed, rolling back</span><br><span class="line">  - Removing kylekatarnls/update-helper (<span class="number">1.1</span><span class="number">.1</span>)</span><br><span class="line"></span><br><span class="line">  [RuntimeException]</span><br><span class="line">  Could not <span class="keyword">delete</span> /home/vagrant/code/w2_support-<span class="regexp">/vendor/</span>kylekatarnls/update-helper/src/   </span><br><span class="line">  UpdateHelper:</span><br><span class="line">  composer install --no-plugins</span><br><span class="line">  我的问题解决了.!https:<span class="comment">//learnku.com/laravel/t/31137</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-FPM-与-Nginx-的通信机制总结"><a href="#PHP-FPM-与-Nginx-的通信机制总结" class="headerlink" title="PHP-FPM 与 Nginx 的通信机制总结"></a>PHP-FPM 与 Nginx 的通信机制总结</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">PHP-FPM 即 PHP-FastCGI Process Manager， 它是 FastCGI 的实现，并提供了进程管理的功能。进程包含 master 进程和 worker 进程两种；master 进程只有一个，负责监听端口，接收来自服务器的请求，而 worker 进程则一般有多个（具体数量根据实际需要进行配置），每个进程内部都会嵌入一个 PHP 解释器，是代码真正执行的地方</span><br><span class="line">https:<span class="comment">//learnku.com/articles/23694</span></span><br><span class="line"></span><br><span class="line">  www.test.com</span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">      Nginx</span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">路由到 www.test.com/index.php</span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">加载 nginx 的 fast-cgi 模块</span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">fast-cgi 监听 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">9000</span> 地址</span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">www.test.com/index.php 请求到达 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">9000</span></span><br><span class="line">        |</span><br><span class="line">        |</span><br><span class="line">     等待处理...</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-报错：PHP-Fatal-error-Uncaught-ReflectionException-Class-request-does-not-exist"><a href="#Laravel-报错：PHP-Fatal-error-Uncaught-ReflectionException-Class-request-does-not-exist" class="headerlink" title="Laravel 报错：PHP Fatal error: Uncaught ReflectionException: Class request does not exist"></a>Laravel 报错：PHP Fatal error: Uncaught ReflectionException: Class request does not exist</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">在 App\Exceptions\Handler::report() 方法里，使用：</span><br><span class="line"></span><br><span class="line">dd($exception);</span><br><span class="line">即可打印更详细的报错信息，知道问题在哪https:<span class="comment">//learnku.com/laravel/t/31178</span></span><br></pre></td></tr></table></figure>
<h3 id="JSON-响应出现浮点小数溢出"><a href="#JSON-响应出现浮点小数溢出" class="headerlink" title="JSON 响应出现浮点小数溢出"></a>JSON 响应出现浮点小数溢出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">调整 php.ini 中 serialize_precision (序列化精度) 的大小来解决这个问题。</span><br><span class="line"></span><br><span class="line">默认值 serialize_precision = <span class="number">-1</span></span><br><span class="line">将这个值改为 小于 <span class="number">17</span> 的数字就解决了这个问题，最后一直往小调，我 <span class="number">14</span> 的时候就没有问题了</span><br><span class="line">https:<span class="comment">//wiki.php.net/rfc/precise_float_value https://learnku.com/articles/31194</span></span><br></pre></td></tr></table></figure>
<h3 id="原码、反码、补码"><a href="#原码、反码、补码" class="headerlink" title="原码、反码、补码"></a>原码、反码、补码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">正数的原码、反码、补码都一样</span><br><span class="line"></span><br><span class="line">负数的反码 = 它的原码符号位不变，其他取反（<span class="number">0</span>-&gt;<span class="number">1</span> , <span class="number">1</span>-&gt;<span class="number">0</span>）</span><br><span class="line"></span><br><span class="line">负数的补码 = 它的反码 + <span class="number">1</span></span><br><span class="line"></span><br><span class="line"> <span class="number">1</span> ==&gt; 原码 [<span class="number">0000</span> <span class="number">0001</span>]   反码[<span class="number">0000</span> <span class="number">0001</span>]   补码[<span class="number">0000</span> <span class="number">0001</span>]</span><br><span class="line"></span><br><span class="line"><span class="number">-1</span> ==&gt; 原码 [<span class="number">1000</span> <span class="number">0001</span>]   反码[<span class="number">1111</span> <span class="number">1110</span>]   补码[<span class="number">1111</span> <span class="number">1111</span>]</span><br><span class="line"><span class="number">0</span> 的反码，补码都是 <span class="number">0</span></span><br><span class="line"></span><br><span class="line">在计算机运算的时候，都是以补码的方式来计算的</span><br><span class="line"></span><br><span class="line"><span class="number">1</span>+<span class="number">1</span>   <span class="number">1</span><span class="number">-1</span> = <span class="number">1</span>+（<span class="number">-1</span>）</span><br><span class="line">按位与 &amp; ： 两位全为１，结果为 <span class="number">1</span>，否则为 <span class="number">0</span></span><br><span class="line"></span><br><span class="line">按位或 | : 两位有一个为 <span class="number">1</span>，结果为 <span class="number">1</span>，否则为 <span class="number">0</span></span><br><span class="line"></span><br><span class="line">按位异或 ^ : 两位一个为 <span class="number">0</span>, 一个为 <span class="number">1</span>，结果为 <span class="number">1</span>，否则为 <span class="number">0</span></span><br><span class="line"><span class="comment">//2&amp;3</span></span><br><span class="line"><span class="comment">//2的补码 0000 0010</span></span><br><span class="line"><span class="comment">//3的补码 0000 0011</span></span><br><span class="line"><span class="comment">//2&amp;3     0000 0010 =&gt;2</span></span><br><span class="line"><span class="comment">//2|3     0000 0011 =&gt;3</span></span><br><span class="line"><span class="comment">//2^3     0000 0001 =&gt;1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//-2^2</span></span><br><span class="line"><span class="comment">//-2的原码 1000 0010</span></span><br><span class="line"><span class="comment">//-2的反码 1111 1101</span></span><br><span class="line"><span class="comment">//-2的补码 1111 1110</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//2 的补码  0000 0010</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//-2^2</span></span><br><span class="line"><span class="comment">//-2的补码 1111 1110</span></span><br><span class="line"><span class="comment">//2 的补码 0000 0010</span></span><br><span class="line"><span class="comment">//-2^2     1111 1100 （补码）</span></span><br><span class="line"><span class="comment">//-2^2     1111 1011 （反码）</span></span><br><span class="line"><span class="comment">//-2^2     1000 0100  （原码） =&gt; 4</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/31179</span></span><br></pre></td></tr></table></figure>
<h3 id="APP-KEY"><a href="#APP-KEY" class="headerlink" title="APP_KEY"></a>APP_KEY</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">只有一个地方会用到 APP_KEY：cookies。Laravel 使用它来加密所有的 cookies，在将 cookie 返回给用户之前 Laravel 会对 cookie 进行加密，然后再返回给用户，这样客户端就无法自己修改 cookie 来伪装成管理员或者其他用户了。</span><br><span class="line"></span><br><span class="line">所以的加密和解密都在 [Encrypter](https:<span class="comment">//learnku.com/docs/laravel/master/encryption#using-the-encrypter) 中进行处理，其中主要使用了 [openssl_encrypt](https://secure.php.net/manual/en/function.openssl-encrypt.php) 进行加密。</span></span><br><span class="line"></span><br><span class="line">有很多用户都会有一个误解，那就是 APP_KEY 是用来处理用户哈希密码的。事实上不是这样的。Laravel 的密码使用了 Hash::make() 或者 bcrypt() 来进行哈希处理，其中并没有用到 APP_KEY</span><br><span class="line">在 Laravel 中有两个主要的加密 Facade，分别是 Crypt（对称加密） 和 Hash（单向加密哈希）。密码是哈希，而 cookie 则是对称加密。</span><br><span class="line">$key = <span class="string">"dont-panic"</span>;</span><br><span class="line">$message = <span class="string">"So long and thanks for all the fish"</span>;</span><br><span class="line">$key = <span class="string">"dont-panic"</span>;</span><br><span class="line">$cipher = <span class="string">"AES-256-CBC"</span>;</span><br><span class="line">echo openssl_encrypt($message, $cipher, $key);</span><br><span class="line"></span><br><span class="line"><span class="comment">// JJEK8L4G3BCfY0evXDRxUke2zqAzq6i7wL/Px4SjaEHXqt3x7hsj4+PhVQaH4ujX</span></span><br><span class="line"></span><br><span class="line">$secret = <span class="string">"JJEK8L4G3BCfY0evXDRxUke2zqAzq6i7wL/Px4SjaEHXqt3x7hsj4+PhVQaH4ujX"</span>;</span><br><span class="line">$key = <span class="string">"dont-panic"</span>;</span><br><span class="line">$cipher = <span class="string">"AES-256-CBC"</span>;</span><br><span class="line">echo openssl_decrypt($secret, $cipher, $key);</span><br><span class="line"></span><br><span class="line"><span class="comment">// So long and thanks for all the fish</span></span><br><span class="line">Laravel 中使用了 PHP 的原生方法 password_hash()，它使用的哈希算法叫 bcrypt。</span><br><span class="line"></span><br><span class="line">use Illuminate\Support\Facades\Hash;</span><br><span class="line"></span><br><span class="line">$password = <span class="string">"dont-panic"</span>;</span><br><span class="line">echo Hash::make($password);</span><br><span class="line"></span><br><span class="line"><span class="comment">// $2y$10$hEEF0lv4spxnvw5O4XyLZ.QjCE1tCu8HjMpWhmCS89J0EcSW0XELu</span></span><br><span class="line">$hash = <span class="string">'$2y$10$hEEF0lv4spxnvw5O4XyLZ.QjCE1tCu8HjMpWhmCS89J0EcSW0XELu'</span>;</span><br><span class="line"><span class="keyword">return</span> Hash::check($input, $hash); https:<span class="comment">//learnku.com/articles/31169</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 加密算法</span></span><br><span class="line"><span class="number">1.</span> $encryptMethod = <span class="string">'aes-256-cbc'</span>;</span><br><span class="line"><span class="comment">// 明文数据</span></span><br><span class="line"><span class="number">2.</span> $data = <span class="string">'Hello World'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 生成IV</span></span><br><span class="line"><span class="number">3.</span> $ivLength = openssl_cipher_iv_length($encryptMethod);</span><br><span class="line"><span class="number">4.</span> $iv = openssl_random_pseudo_bytes($ivLength, $isStrong);</span><br><span class="line"><span class="number">5.</span> <span class="keyword">if</span> (<span class="literal">false</span> === $iv &amp;&amp; <span class="literal">false</span> === $isStrong) &#123;</span><br><span class="line"><span class="number">6.</span>     die(<span class="string">'IV generate failed'</span>);</span><br><span class="line"><span class="number">7.</span> &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加密</span></span><br><span class="line"><span class="number">8.</span> $encrypted = openssl_encrypt($data, $encryptMethod, <span class="string">'secret'</span>, <span class="number">0</span>, $iv);</span><br><span class="line"><span class="comment">// 解密</span></span><br><span class="line"><span class="number">9.</span> $decrypted = openssl_decrypt($encrypted, $encryptMethod, <span class="string">'secret'</span>, <span class="number">0</span>, $iv);</span><br></pre></td></tr></table></figure>
<h3 id="PHP-识别相片"><a href="#PHP-识别相片" class="headerlink" title="PHP 识别相片"></a>PHP 识别相片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">PHP 识别相片是否是颠倒的，并且重新摆正相片https:<span class="comment">//learnku.com/articles/31235</span></span><br><span class="line">$image = imagecreatefromstring(file_get_contents($_FILES[<span class="string">'image_upload'</span>][<span class="string">'tmp_name'</span>]));</span><br><span class="line">$exif = exif_read_data($_FILES[<span class="string">'image_upload'</span>][<span class="string">'tmp_name'</span>]);</span><br><span class="line"><span class="keyword">if</span>(!empty($exif[<span class="string">'Orientation'</span>])) &#123;</span><br><span class="line">    <span class="keyword">switch</span>($exif[<span class="string">'Orientation'</span>]) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">8</span>:</span><br><span class="line">            $image = imagerotate($image,<span class="number">90</span>,<span class="number">0</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            $image = imagerotate($image,<span class="number">180</span>,<span class="number">0</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">            $image = imagerotate($image,<span class="number">-90</span>,<span class="number">0</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Composer-镜像限额，出现要求用户名认证"><a href="#Composer-镜像限额，出现要求用户名认证" class="headerlink" title="Composer 镜像限额，出现要求用户名认证"></a>Composer 镜像限额，出现要求用户名认证</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">从 GitHub 下载 Zip 代码包而不是 CDN：https:<span class="comment">//learnku.com/articles/30415</span></span><br><span class="line"></span><br><span class="line">$ composer install --prefer-source</span><br><span class="line">不要在框架的 composer.json 里写死了源</span><br></pre></td></tr></table></figure>
<h3 id="AB-压测"><a href="#AB-压测" class="headerlink" title="AB 压测"></a>AB 压测</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">ab -c <span class="number">20</span> -t <span class="number">10</span> -k  -T <span class="string">"application/json"</span> -H <span class="string">"Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LnpoemhlLmNvbSIsImlhdCI6MTU2MzE4MjA1OCwiZXhwIjoxNTY2MzM1NjU5LCJuYmYiOjE1NjMxODIwNTksImp0aSI6IlRiYXR5amhpMGJ6eHRNeEciLCJzdWIiOjEsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.PV-S7b_fbSPgtAk-6r-cDqGkoFL-UOsGJWVfQUV6p5E"</span>   http:<span class="comment">//ys.test/api/articles</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/31291</span></span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next, $maxAttempts = <span class="number">60</span>, $decayMinutes = <span class="number">1</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $key = $<span class="keyword">this</span>-&gt;resolveRequestSignature($request);</span><br><span class="line">        $maxAttempts = $<span class="keyword">this</span>-&gt;resolveMaxAttempts($request, $maxAttempts);</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;limiter-&gt;tooManyAttempts($key, $maxAttempts)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> $<span class="keyword">this</span>-&gt;buildException($key, $maxAttempts);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;limiter-&gt;hit($key, $decayMinutes);</span><br><span class="line"></span><br><span class="line">        $response = $next($request);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;addHeaders(</span><br><span class="line">            $response, $maxAttempts,</span><br><span class="line">            $<span class="keyword">this</span>-&gt;calculateRemainingAttempts($key, $maxAttempts)</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">ThrottleRequests 中间件中 <span class="number">60</span>和<span class="number">1</span> 的意思是一分钟限制 <span class="number">60</span> 次。 把客户端请求的请求源 ip 啥的，加密成 <span class="number">1</span> 个 key（代表一个独立的请求对象），存储在缓存中，如果一分钟内超过这个限制，那么服务器就抛出 <span class="number">429</span>: Too Many Attempts.</span><br><span class="line">并且在头部回传两个个响应头: X-RateLimit-Limit（限制次数）, X-RateLimit-Remaining (剩下多少次)</span><br></pre></td></tr></table></figure>
<h3 id="php爬虫框架Goutte"><a href="#php爬虫框架Goutte" class="headerlink" title="php爬虫框架Goutte"></a>php爬虫框架Goutte</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> fabpot/goutte</span><br><span class="line">use Goutte\Client;</span><br><span class="line">$client = <span class="keyword">new</span> Client();</span><br><span class="line">使用request()方法发起页面请求</span><br><span class="line"></span><br><span class="line"><span class="comment">// Go to the symfony.com website</span></span><br><span class="line">$crawler = $client-&gt;request(<span class="string">'GET'</span>, <span class="string">'https://www.symfony.com/blog/'</span>);</span><br><span class="line">该方法会返回一个Crawler对象(Symfony\Component\DomCrawler\Crawler).</span><br><span class="line"></span><br><span class="line">Goutte依赖Guzzle，如果要自己配置Guzzle参数的话你可以为Goutte创建一个Guzzle实例，下面我们要做一个<span class="number">60</span>秒请求超时时间，并且无需验证ssl的设定:</span><br><span class="line"></span><br><span class="line">use Goutte\Client;</span><br><span class="line">use GuzzleHttp\Client <span class="keyword">as</span> GuzzleClient;</span><br><span class="line"></span><br><span class="line">$goutteClient = <span class="keyword">new</span> Client();</span><br><span class="line">$guzzleClient = <span class="keyword">new</span> GuzzleClient(array(</span><br><span class="line">    <span class="string">'timeout'</span> =&gt; <span class="number">60</span>,</span><br><span class="line">    <span class="string">'verify'</span> =&gt; <span class="literal">false</span>,    <span class="comment">//官方example中没有这项,这个还是很必要的,不然在请求有些https站点的时候会出错,我已填坑</span></span><br><span class="line">));</span><br><span class="line">$goutteClient-&gt;setClient($guzzleClient);</span><br><span class="line">点击链接</span><br><span class="line"></span><br><span class="line"><span class="comment">// Click on the "Security Advisories" link</span></span><br><span class="line">$link = $crawler-&gt;selectLink(<span class="string">'Security Advisories'</span>)-&gt;link();</span><br><span class="line">$crawler = $client-&gt;click($link);</span><br><span class="line">提取数据</span><br><span class="line"></span><br><span class="line"><span class="comment">// Get the latest post in this category and display the titles</span></span><br><span class="line">$crawler-&gt;filter(<span class="string">'h2 &gt; a'</span>)-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$node</span>) </span>&#123;</span><br><span class="line">    print $node-&gt;text().<span class="string">"\n"</span>;</span><br><span class="line">&#125;);</span><br><span class="line">表单提交https:<span class="comment">//towait.com/blog/php-web-crawler-goutte/</span></span><br><span class="line"></span><br><span class="line">$crawler = $client-&gt;request(<span class="string">'GET'</span>, <span class="string">'https://github.com/'</span>);</span><br><span class="line">$crawler = $client-&gt;click($crawler-&gt;selectLink(<span class="string">'Sign in'</span>)-&gt;link());</span><br><span class="line">$form = $crawler-&gt;selectButton(<span class="string">'Sign in'</span>)-&gt;form();</span><br><span class="line">$crawler = $client-&gt;submit($form, array(<span class="string">'login'</span> =&gt; <span class="string">'fabpot'</span>, <span class="string">'password'</span> =&gt; <span class="string">'xxxxxx'</span>));</span><br><span class="line">$crawler-&gt;filter(<span class="string">'.flash-error'</span>)-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$node</span>) </span>&#123;</span><br><span class="line">    print $node-&gt;text().<span class="string">"\n"</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="防恶意频繁提交"><a href="#防恶意频繁提交" class="headerlink" title="防恶意频繁提交"></a>防恶意频繁提交</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取和校验ip；同时防止短时间内多次提交</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @notice        ：弹出验证码，需要替换掉echo $echo_str 即可。</span></span><br><span class="line"><span class="comment"> * @return string ：返回校验成功的ip</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">getAndCheckIP</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取环境ip</span></span><br><span class="line">    <span class="keyword">if</span> (getenv(<span class="string">"HTTP_CLIENT_IP"</span>) &amp;&amp; strcasecmp(getenv(<span class="string">"HTTP_CLIENT_IP"</span>), <span class="string">"unknown"</span>))</span><br><span class="line">        $ip = getenv(<span class="string">"HTTP_CLIENT_IP"</span>);</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (getenv(<span class="string">"HTTP_X_FORWARDED_FOR"</span>) &amp;&amp; strcasecmp(getenv(<span class="string">"HTTP_X_FORWARDED_FOR"</span>), <span class="string">"unknown"</span>))</span><br><span class="line">        $ip = getenv(<span class="string">"HTTP_X_FORWARDED_FOR"</span>);</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (getenv(<span class="string">"REMOTE_ADDR"</span>) &amp;&amp; strcasecmp(getenv(<span class="string">"REMOTE_ADDR"</span>), <span class="string">"unknown"</span>))</span><br><span class="line">        $ip = getenv(<span class="string">"REMOTE_ADDR"</span>);</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (isset($_SERVER[<span class="string">'REMOTE_ADDR'</span>]) &amp;&amp; $_SERVER[<span class="string">'REMOTE_ADDR'</span>] &amp;&amp; strcasecmp($_SERVER[<span class="string">'REMOTE_ADDR'</span>], <span class="string">"unknown"</span>))</span><br><span class="line">        $ip = $_SERVER[<span class="string">'REMOTE_ADDR'</span>];</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        $ip = <span class="string">"unknown"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// check 环境ip</span></span><br><span class="line">    <span class="keyword">if</span> (!$<span class="keyword">this</span>-&gt;isWhiteList($ip)) &#123;</span><br><span class="line">        $echo_str = <span class="string">"提交过于频繁,请稍后再试！"</span>;</span><br><span class="line">        <span class="comment">// 构建ip的时间栈数据</span></span><br><span class="line">        <span class="keyword">if</span> (!is_array($_SESSION[$ip])) &#123;</span><br><span class="line">            $_SESSION[$ip] = array();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">0</span>])) &#123;</span><br><span class="line">            $_SESSION[$ip][] = time();</span><br><span class="line"></span><br><span class="line">            <span class="comment">// session 保存时间为6小时。清理session</span></span><br><span class="line">            $post_interval_first = time() - $_SESSION[$ip][<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">if</span> ($post_interval_first &gt; <span class="number">21600</span>) &#123;</span><br><span class="line">                $_SESSION[$ip] = array();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 两次提交小于1s，禁止提交</span></span><br><span class="line">            $post_interval_pre = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> ($post_interval_pre &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 您在10s内已经提交了3请求，禁止提交</span></span><br><span class="line">            $post_interval_third = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">3</span>]) &amp;&amp; ($post_interval_third &lt; <span class="number">10</span>)) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 您在1分钟期间已经提交了5请求，禁止提交</span></span><br><span class="line">            $post_interval_fifth = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">5</span>]) &amp;&amp; ($post_interval_fifth &lt; <span class="number">60</span>)) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 6小时内提交10次，禁止提交</span></span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">10</span>])) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $_SESSION[$ip][] = time();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> ($ip);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 检验是否存在于白名单中</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param $ip    ：校验的ip</span></span><br><span class="line"><span class="comment"> * @return bool  ：校验结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isWhiteList</span>(<span class="params">$ip</span>)</span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 内网ip默认全部存在于白名单中</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">if</span>(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 是否在写死的whitelist 里面</span></span><br><span class="line">    <span class="keyword">return</span> in_array($ip,$<span class="keyword">this</span>-&gt;_WHTTE_LIST);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Goutte爬取-IP-代理池"><a href="#Goutte爬取-IP-代理池" class="headerlink" title="Goutte爬取 IP 代理池"></a>Goutte爬取 IP 代理池</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getProxyIPPool</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $client = <span class="keyword">new</span> Client();</span><br><span class="line">    $ipPoolUrl = <span class="string">'https://proxy.coderbusy.com/'</span>;</span><br><span class="line">    $crawler = $client-&gt;request(<span class="string">'GET'</span>, $ipPoolUrl);</span><br><span class="line">    $ipArr = $crawler-&gt;filter(<span class="string">'table'</span>)-&gt;filter(<span class="string">'tr'</span>)-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$tr, $i</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $tr-&gt;filter(<span class="string">'td'</span>)-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$td, $i</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> trim($td-&gt;text());</span><br><span class="line"></span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $newArr = [];</span><br><span class="line">    foreach ($ipArr <span class="keyword">as</span> $key =&gt;$value)&#123;</span><br><span class="line">        <span class="keyword">if</span> ($key &gt;<span class="number">0</span>)&#123;</span><br><span class="line">            $newArr[$key<span class="number">-1</span>] = $value[<span class="number">0</span>].<span class="string">':'</span>.$value[<span class="number">1</span>];<span class="comment">//这里就是 table 的第一列和第二列</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    $file = file_put_contents(<span class="string">'ipPool.json'</span>, json_encode($newArr));</span><br><span class="line"></span><br><span class="line">    echo <span class="string">"更新代理 IP 尺完毕"</span>;</span><br><span class="line">&#125;</span><br><span class="line">[</span><br><span class="line">    <span class="string">"13.59.33.106:3128"</span>,</span><br><span class="line">    <span class="string">"200.116.227.99:53281"</span>,</span><br><span class="line">    <span class="string">"190.232.168.242:8080"</span></span><br><span class="line">]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">voteByIP</span> (<span class="params">$ip, $port</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    $post_data = array (<span class="string">"bookid"</span> =&gt; <span class="string">"102428"</span>);</span><br><span class="line">    $ch = curl_init(<span class="string">"http://jljapi.manhuadao.cn/Vote/vote"</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_POST, <span class="number">1</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);</span><br><span class="line">    curl_setopt($ch, CURLOPT_RETURNTRANSFER,<span class="number">1</span>);</span><br><span class="line">    curl_setopt($ch,CURLOPT_PROXY,$ip);</span><br><span class="line">    curl_setopt($ch,CURLOPT_PROXYPORT,$port);</span><br><span class="line">    curl_setopt ($ch, CURLOPT_TIMEOUT, <span class="number">1200</span>);</span><br><span class="line"></span><br><span class="line">    $result = curl_exec($ch);</span><br><span class="line">   print_r($result.PHP_EOL);</span><br><span class="line">    curl_close($ch);</span><br><span class="line">&#125;</span><br><span class="line">$ipPoolArr = json_decode(file_get_contents(<span class="string">'ipPool.json'</span>));</span><br><span class="line"></span><br><span class="line">foreach ($ipPoolArr <span class="keyword">as</span> $key=&gt;$proxyStr)&#123;</span><br><span class="line">     $ip = $proxyArr[<span class="number">0</span>];</span><br><span class="line">    $port = $proxyArr[<span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">    voteByIP($ip, $port)</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>((i=<span class="number">1</span>;i&lt;=<span class="number">99999999999999999999999999999999999999</span>;i++)); </span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">     php autoVote.php</span><br><span class="line"></span><br><span class="line">     <span class="comment">//还可以执行其他的操作，例如 php getIPPool.php 来更新代理 IP 池 https://qimajiang.com/2017/11/18/%E4%BD%BF%E7%94%A8%20Goutte%E7%88%AC%E5%8F%96%E4%BB%A3%E7%90%86%20IP%20%E6%B1%A0/</span></span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="微软小冰接口的颜值检测"><a href="#微软小冰接口的颜值检测" class="headerlink" title="微软小冰接口的颜值检测"></a>微软小冰接口的颜值检测</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$face = <span class="keyword">new</span> \Hanson\Face\Foundation\Face();</span><br><span class="line"></span><br><span class="line">$result = $face-&gt;score-&gt;get(<span class="string">'https://ws2.sinaimg.cn/large/685b97a1gy1fehkmbi6hvj20u00u07ab.jpg'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**https://github.com/HanSon/face</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">[</span></span><br><span class="line"><span class="comment">    'score' =&gt; 6.8,</span></span><br><span class="line"><span class="comment">    'text' =&gt; '哥们颜值才6.8分，一下让整体颜值从7.3跌到7.1ORZ',</span></span><br><span class="line"><span class="comment">    'url' =&gt; 'http://mediaplatform.trafficmanager.cn/image/fetchimage?key='</span></span><br><span class="line"><span class="comment">];</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">**/</span></span><br></pre></td></tr></table></figure>
<h3 id="php-fpm-max-children-优化"><a href="#php-fpm-max-children-优化" class="headerlink" title="php-fpm max_children 优化"></a>php-fpm max_children 优化</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> ps -ylef --sort:rss 此命令可以查看当前服务器所有的进程以及占用内存，留意 RSS 那列就是占用的内存数 http:<span class="comment">//hanc.cc/index.php/archives/179/</span></span><br><span class="line"> S UID        PID  PPID  C PRI  NI   RSS    SZ WCHAN  STIME TTY          TIME CMD</span><br><span class="line"> S root         <span class="number">2</span>     <span class="number">0</span>  <span class="number">0</span>  <span class="number">80</span>   <span class="number">0</span>     <span class="number">0</span>     <span class="number">0</span> kthrea May05 ?        <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> [kthreadd]</span><br><span class="line">S finance  <span class="number">16670</span> <span class="number">16668</span>  <span class="number">0</span>  <span class="number">80</span>   <span class="number">0</span>  <span class="number">7424</span> <span class="number">91470</span> inet_c Jul16 ?        <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> php-fpm: pool www</span><br><span class="line">S root     <span class="number">16668</span>     <span class="number">1</span>  <span class="number">0</span>  <span class="number">80</span>   <span class="number">0</span>  <span class="number">8184</span> <span class="number">91470</span> ep_pol Jul16 ?        <span class="number">00</span>:<span class="number">00</span>:<span class="number">04</span> php-fpm: master process (<span class="regexp">/data0/</span>opt/php55/etc/php-fpm.conf)</span><br><span class="line">S root     <span class="number">16677</span>     <span class="number">1</span>  <span class="number">0</span>  <span class="number">80</span>   <span class="number">0</span>  <span class="number">8932</span> <span class="number">98194</span> ep_pol Jul16 ?        <span class="number">00</span>:<span class="number">00</span>:<span class="number">09</span> php-fpm: master process (<span class="regexp">/data0/</span>opt/php7/etc/php-fpm.conf)</span><br><span class="line">S root     <span class="number">16714</span>     <span class="number">1</span>  <span class="number">0</span>  <span class="number">80</span>   <span class="number">0</span> <span class="number">11140</span> <span class="number">97479</span> ep_pol Jul16 ?        <span class="number">00</span>:<span class="number">00</span>:<span class="number">04</span> php-fpm: master process (<span class="regexp">/data0/</span>opt/php54/etc/php-fpm.conf)</span><br></pre></td></tr></table></figure>
<h3 id="使用多个同一数据库驱动"><a href="#使用多个同一数据库驱动" class="headerlink" title="使用多个同一数据库驱动"></a>使用多个同一数据库驱动</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">新项目需要连接两个mysql数据库，然而却出现无论如何也查询不了第二个数据库的情况。</span><br><span class="line"></span><br><span class="line">经历了多次断点调试，google无止境搜索，询问印度阿三无果的情况下，也只能查看源码解决问题了。</span><br><span class="line"></span><br><span class="line">最终发现在laravel的ConnectionFactory类的createConnection方法中有这么一段</span><br><span class="line"></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">createConnection</span>(<span class="params">$driver, $connection, $database, $prefix = <span class="string">''</span>, array $config = []</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;container-&gt;bound($key = <span class="string">"db.connection.&#123;$driver&#125;"</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;container-&gt;make($key, [$connection, $database, $prefix, $config]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">switch</span> ($driver) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'mysql'</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> MySqlConnection($connection, $database, $prefix, $config);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'pgsql'</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> PostgresConnection($connection, $database, $prefix, $config);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'sqlite'</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> SQLiteConnection($connection, $database, $prefix, $config);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'sqlsrv'</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> SqlServerConnection($connection, $database, $prefix, $config);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> InvalidArgumentException(<span class="string">"Unsupported driver [$driver]"</span>);</span><br><span class="line">&#125;</span><br><span class="line">$<span class="keyword">this</span>-&gt;container-&gt;bound($key = <span class="string">"db.connection.&#123;$driver&#125;"</span>) 通过driver去跟容器做了绑定，也就是当你有第二个一样driver的数据库时，调用 $<span class="keyword">this</span>-&gt;container-&gt;make()方法也会同样返回第一个绑定的 connection，这样无论如何也只能查询第一个了。http:<span class="comment">//hanc.cc/index.php/archives/153/</span></span><br><span class="line"></span><br><span class="line">修改后</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;container-&gt;bound($key = <span class="string">"db.connection.&#123;$driver&#125;.&#123;$config['name']&#125;"</span>)) &#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;container-&gt;make($key, [$connection, $database, $prefix, $config]);</span><br><span class="line">&#125;</span><br><span class="line">通过传入connection自定义的名称，能够避免重复</span><br></pre></td></tr></table></figure>
<h3 id="PHP的MD5加密与JAVA的MD5加密不一致"><a href="#PHP的MD5加密与JAVA的MD5加密不一致" class="headerlink" title="PHP的MD5加密与JAVA的MD5加密不一致"></a>PHP的MD5加密与JAVA的MD5加密不一致</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">android端正确的MD5加密代码</span><br><span class="line"></span><br><span class="line">public <span class="keyword">static</span> <span class="built_in">String</span> md5(byte[] source) &#123;</span><br><span class="line">    <span class="keyword">try</span>&#123;</span><br><span class="line">        MessageDigest md = MessageDigest.getInstance(<span class="string">"MD5"</span>);</span><br><span class="line">        md.update( source );</span><br><span class="line">        StringBuffer buf=<span class="keyword">new</span> StringBuffer();</span><br><span class="line">        <span class="keyword">for</span>(byte b:md.digest())</span><br><span class="line">            buf.append(<span class="built_in">String</span>.format(<span class="string">"%02x"</span>, b&amp;<span class="number">0xff</span>) );</span><br><span class="line">        <span class="keyword">return</span> buf.toString();</span><br><span class="line">    &#125;<span class="keyword">catch</span>( Exception e )&#123;</span><br><span class="line">        e.printStackTrace(); <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">从代码可以看出，这里传入的参数是byte[]，而不是<span class="built_in">String</span>，这里就需要事先对需要加密的字符串转成</span><br><span class="line"></span><br><span class="line">字节，便用到一个方法getBytes(Charset.forName(<span class="string">"UTF-8"</span>))http:<span class="comment">//hanc.cc/index.php/archives/14/</span></span><br></pre></td></tr></table></figure>
<h3 id="生成条形码"><a href="#生成条形码" class="headerlink" title="生成条形码"></a>生成条形码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> codeitnowin/barcode</span><br><span class="line">namespace App\Http\Controllers;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line"></span><br><span class="line">use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;</span><br><span class="line">use CodeItNow\BarcodeBundle\Utils\QrCode;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BarCodeController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - QrCode&lt;/p&gt;'</span>;</span><br><span class="line">        $qrCode = <span class="keyword">new</span> QrCode();</span><br><span class="line">        $qrCode</span><br><span class="line">            -&gt;setText(<span class="string">'https://www.php.net/manual/zh/'</span>)</span><br><span class="line">            -&gt;setSize(<span class="number">300</span>)</span><br><span class="line">            -&gt;setPadding(<span class="number">10</span>)</span><br><span class="line">            -&gt;setErrorCorrection(<span class="string">'high'</span>)</span><br><span class="line">            -&gt;setForegroundColor(array(<span class="string">'r'</span> =&gt; <span class="number">0</span>, <span class="string">'g'</span> =&gt; <span class="number">0</span>, <span class="string">'b'</span> =&gt; <span class="number">0</span>, <span class="string">'a'</span> =&gt; <span class="number">0</span>))</span><br><span class="line">            -&gt;setBackgroundColor(array(<span class="string">'r'</span> =&gt; <span class="number">255</span>, <span class="string">'g'</span> =&gt; <span class="number">255</span>, <span class="string">'b'</span> =&gt; <span class="number">255</span>, <span class="string">'a'</span> =&gt; <span class="number">0</span>))</span><br><span class="line">            -&gt;setLabel(<span class="string">'https://www.php.net/'</span>)</span><br><span class="line">            -&gt;setLabelFontSize(<span class="number">16</span>)</span><br><span class="line">            -&gt;setImageType(QrCode::IMAGE_TYPE_PNG);</span><br><span class="line">        echo <span class="string">'&lt;img src="data:'</span> . $qrCode-&gt;getContentType() . <span class="string">';base64,'</span> . $qrCode-&gt;generate() . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Code128&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0123456789"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Code128);</span><br><span class="line">        $barcode-&gt;setScale(<span class="number">2</span>);</span><br><span class="line">        $barcode-&gt;setThickness(<span class="number">25</span>);</span><br><span class="line">        $barcode-&gt;setFontSize(<span class="number">10</span>);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Code11&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0123456789"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Code11);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Code39&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0123456789"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Code39);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Code39Extended&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0123456789"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Code39Extended);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Ean128&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"00123456789012345675"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Ean128);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Gs1128&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"00123456789012345675"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Gs1128);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Gs1128&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"4157707266014651802001012603068039000000006377069620171215"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Gs1128);</span><br><span class="line">        $barcode-&gt;setNoLengthLimit(<span class="literal">true</span>);</span><br><span class="line">        $barcode-&gt;setAllowsUnknownIdentifier(<span class="literal">true</span>);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// i15为偶数</span></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - I25&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"00123456789012345675"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::I25);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Isbn&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0012345678901"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Isbn);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Msi&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"0012345678901"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Msi);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Postnet&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"01234567890"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Postnet);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - S25&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"012345678901"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::S25);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Upca&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"012345678901"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Upca);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">        echo <span class="string">'&lt;hr&gt;'</span>;</span><br><span class="line">        echo <span class="string">'&lt;p&gt;Example - Upce&lt;/p&gt;'</span>;</span><br><span class="line">        $barcode = <span class="keyword">new</span> BarcodeGenerator();</span><br><span class="line">        $barcode-&gt;setText(<span class="string">"012345"</span>);</span><br><span class="line">        $barcode-&gt;setType(BarcodeGenerator::Upce);</span><br><span class="line">        $code = $barcode-&gt;generate();</span><br><span class="line">        echo <span class="string">'&lt;img src="data:image/png;base64,'</span> . $code . <span class="string">'" /&gt;'</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/31325</span></span><br></pre></td></tr></table></figure>
<h3 id="Composer-输入账号密码"><a href="#Composer-输入账号密码" class="headerlink" title="Composer 输入账号密码"></a>Composer 输入账号密码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">把 composer.lock 文件， vendor 目录删掉。 然后</span><br><span class="line"></span><br><span class="line">composer global clearcache</span><br><span class="line"></span><br><span class="line">composer clearcache</span><br></pre></td></tr></table></figure>
<h3 id="foreach-引用"><a href="#foreach-引用" class="headerlink" title="foreach 引用"></a>foreach 引用</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$array = [<span class="string">'zero'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>];</span><br><span class="line">$value = &amp;$array[<span class="number">3</span>];</span><br><span class="line"><span class="comment">// foreach 的作用如上</span></span><br><span class="line"><span class="comment">// $value 现在是$array[3]的地址</span></span><br><span class="line">$value = <span class="number">100</span>;</span><br><span class="line"><span class="comment">// 相当于 $array[3] 的内存地址为 100</span></span><br><span class="line"></span><br><span class="line">dump($array);</span><br><span class="line"></span><br><span class="line">array:<span class="number">4</span> [▼</span><br><span class="line">  <span class="number">0</span> =&gt; <span class="string">"zero"</span></span><br><span class="line">  <span class="number">1</span> =&gt; <span class="string">"one"</span></span><br><span class="line">  <span class="number">2</span> =&gt; <span class="string">"two"</span></span><br><span class="line">  <span class="number">3</span> =&gt; &amp; <span class="number">100</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="获取日期数组"><a href="#获取日期数组" class="headerlink" title="获取日期数组"></a>获取日期数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getDateRange</span>(<span class="params">$startDate, $endDate</span>)</span>&#123;</span><br><span class="line">	$startTimeStamp = strtotime($startDate);</span><br><span class="line">	$endTimeStamp = strtotime($endDate);</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 计算日期段内有多少天</span></span><br><span class="line">	$days = ($endTimeStamp-$startTimeStamp) / <span class="number">86400</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 保存每天日期</span></span><br><span class="line">	$date = array();</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>($i=<span class="number">0</span>; $i&lt;$days; $i++)&#123;</span><br><span class="line">		$date[] = date(<span class="string">'Y-m-d'</span>, $startTimeStamp+(<span class="number">86400</span>*$i));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> $date;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$list = getDateRange(<span class="string">'2018/01/01'</span>, <span class="string">'2018/01/03);</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">/**</span></span><br><span class="line"><span class="string"> * $list 如下 </span></span><br><span class="line"><span class="string"> * [2018-01-01,2018-01-02,2018-01-03]</span></span><br><span class="line"><span class="string"> */</span></span><br></pre></td></tr></table></figure>
<h3 id="女神QQ号码"><a href="#女神QQ号码" class="headerlink" title="女神QQ号码"></a>女神QQ号码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">取整=&gt;取余=&gt;取余*<span class="number">10</span>+取整。。。。。取余取整的对象都是<span class="number">10</span>的倍数，根据位数而定，每次取整之后就是一位，循环直到数字等于<span class="number">0</span>。</span><br><span class="line">$raw_num = <span class="number">631758924</span>;</span><br><span class="line">$num = <span class="number">0</span>;</span><br><span class="line">$devisor = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span>($devisor &lt; $raw_num)</span><br><span class="line">&#123;</span><br><span class="line">    $devisor *= <span class="number">10</span>;     <span class="comment">//获取最小的大于raw_num的10的倍数的整数https://www.cnblogs.com/iforever/p/4584490.html</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> ($raw_num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    $devisor /= <span class="number">10</span>;</span><br><span class="line">    $next = floor($raw_num / $devisor); <span class="comment">//获取下一个数字</span></span><br><span class="line">    $num = $num*<span class="number">10</span> + $next;             <span class="comment">//计算”半成品“QQ号码</span></span><br><span class="line">    $raw_num = $raw_num % $devisor;</span><br><span class="line">    $last = floor($raw_num * <span class="number">10</span> / $devisor);    <span class="comment">//移动数字，拼接最新的QQ号码</span></span><br><span class="line"></span><br><span class="line">    $pre = $raw_num % (ceil($devisor / <span class="number">10</span>));</span><br><span class="line"></span><br><span class="line">    $raw_num = $pre * <span class="number">10</span> + $last;   </span><br><span class="line">&#125;</span><br><span class="line">echo <span class="string">"恭喜你啦，成功获取QQ号码：&#123;$num&#125;"</span>;    <span class="comment">//恭喜你啦，成功获取QQ号码：615947283</span></span><br><span class="line"><span class="keyword">var</span> n = <span class="string">"631758924"</span>;</span><br><span class="line"><span class="keyword">var</span> arr = [];</span><br><span class="line"><span class="keyword">var</span> res = [];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;n.length; i++) &#123;</span><br><span class="line">    arr.push(n.charAt(i));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(arr.length) &#123;</span><br><span class="line">    <span class="keyword">if</span>(arr.length !== <span class="number">1</span>) &#123;</span><br><span class="line">        res.push( arr.shift() );</span><br><span class="line">        arr.push( arr.shift() );    </span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        res.push( arr.shift() );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;   </span><br><span class="line"><span class="built_in">console</span>.log(res.join(<span class="string">''</span>)); <span class="comment">//615947283</span></span><br><span class="line">&gt;&gt;&gt; qq = lambda e:len(e)&gt;<span class="number">1</span> and e[<span class="number">0</span>]+qq(e[<span class="number">2</span>:]+e[<span class="number">1</span>]) or e</span><br><span class="line">&gt;&gt;&gt; print qq(<span class="string">'631758924'</span>)</span><br><span class="line"><span class="number">615947283</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP实现linux命令tail-f"><a href="#PHP实现linux命令tail-f" class="headerlink" title="PHP实现linux命令tail -f"></a>PHP实现linux命令tail -f</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/env php </span></span><br><span class="line">&lt;?php</span><br><span class="line"><span class="keyword">if</span>(<span class="number">2</span> != count($argv))&#123;</span><br><span class="line">    fwrite(</span><br><span class="line">        STDERR,</span><br><span class="line">        <span class="string">"调用格式错误！使用格式 ./tail filename"</span>.PHP_EOL</span><br><span class="line">    );  </span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://www.cnblogs.com/iforever/p/5202197.html</span></span><br><span class="line">$file_name      = $argv[<span class="number">1</span>];</span><br><span class="line">define(<span class="string">"MAX_SHOW"</span>, <span class="number">8192</span>);<span class="comment">//当内容长度超过这个阈值的时候，只输出最后面的8192个字节，这样就不会出现大面积的刷新导致看不清的问题</span></span><br><span class="line"></span><br><span class="line">$file_size      = <span class="number">0</span>;</span><br><span class="line">$file_size_new  = <span class="number">0</span>;</span><br><span class="line">$add_size       = <span class="number">0</span>;</span><br><span class="line">$ignore_size    = <span class="number">0</span>;</span><br><span class="line">$fp = fopen($file_name, <span class="string">"r"</span>);</span><br><span class="line"><span class="keyword">while</span>(<span class="number">1</span>)&#123;</span><br><span class="line">    clearstatcache();<span class="comment">//php中获取文件大小之前一定要运行函数clearstatcache()，清除文件状态缓存，否则获取文件大小可能会有偏差</span></span><br><span class="line">    $file_size_new  = filesize($file_name);</span><br><span class="line">    $add_size       = $file_size_new - $file_size;</span><br><span class="line">    <span class="keyword">if</span>($add_size &gt; <span class="number">0</span>)&#123; </span><br><span class="line">        <span class="keyword">if</span>($add_size &gt; MAX_SHOW)&#123;</span><br><span class="line">            $ignore_size    = $add_size - MAX_SHOW;</span><br><span class="line">            $add_size       = MAX_SHOW;</span><br><span class="line">            fseek($fp, $file_size + $ignore_size);</span><br><span class="line">        &#125;   </span><br><span class="line">        fwrite(</span><br><span class="line">            STDOUT,</span><br><span class="line">            fread($fp, $add_size)</span><br><span class="line">        );  </span><br><span class="line">        $file_size  = $file_size_new;</span><br><span class="line">    &#125;</span><br><span class="line">    usleep(<span class="number">50000</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fclose($fp);</span><br></pre></td></tr></table></figure>
<h3 id="Unicode到UTF-8"><a href="#Unicode到UTF-8" class="headerlink" title="Unicode到UTF-8"></a>Unicode到UTF-8</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">汉   =&gt; <span class="function"><span class="params">E6B189</span> =&gt;</span> <span class="number">11100110</span> <span class="number">10110001</span> <span class="number">10001001</span> =&gt; <span class="number">01101100</span> <span class="number">01001001</span> =&gt; <span class="number">6</span>C49</span><br><span class="line">字   =&gt; <span class="function"><span class="params">E5AD97</span> =&gt;</span> <span class="number">11100101</span> <span class="number">10101101</span> <span class="number">10010111</span> =&gt; <span class="number">01011011</span> <span class="number">01010111</span> =&gt; <span class="number">5</span>B57</span><br><span class="line"></span><br><span class="line">#下面是在chrome命令行下面运行的结果</span><br><span class="line"><span class="string">'\u6C49'</span></span><br><span class="line"><span class="string">"汉"</span></span><br><span class="line"><span class="string">'\u5B57'</span></span><br><span class="line"><span class="string">"字"</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">utf8_bytes</span>(<span class="params">str</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="number">0</span>, unicode;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str.length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        unicode = str.charCodeAt(i);</span><br><span class="line">        <span class="keyword">if</span>(unicode &lt; <span class="number">0x0080</span>) &#123;</span><br><span class="line">            ++len;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(unicode &lt; <span class="number">0x0800</span>) &#123;</span><br><span class="line">            len += <span class="number">2</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(unicode &lt;= <span class="number">0xFFFF</span>) &#123;</span><br><span class="line">            len += <span class="number">3</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="string">"characters must be USC-2!!"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> len;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#例子</span><br><span class="line">utf8_bytes(<span class="string">'asdasdas'</span>)</span><br><span class="line"><span class="number">8</span></span><br><span class="line">utf8_bytes(<span class="string">'yrt燕睿涛'</span>)</span><br><span class="line"><span class="number">12</span></span><br><span class="line">#对于GBK字符串https://www.cnblogs.com/iforever/p/4520692.html</span><br><span class="line">$len = ceil(strlen(bin2hex(iconv(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, $word)))/<span class="number">2</span>);</span><br><span class="line">#对于UTF8字符串</span><br><span class="line">$len = ceil(strlen(bin2hex($word))/<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<h3 id="PDO-防止-sql-注入"><a href="#PDO-防止-sql-注入" class="headerlink" title="PDO 防止 sql 注入"></a>PDO 防止 sql 注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$user=$_POST[<span class="string">'user'</span>]; $pass=$_POST[<span class="string">'pass'</span>];</span><br><span class="line">  $dbh = <span class="keyword">new</span> \PDO(<span class="string">"mysql:host=localhost; dbname=zz"</span>, <span class="string">"root"</span>, <span class="string">"root"</span>);</span><br><span class="line">  $dbh-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, <span class="literal">false</span>);</span><br><span class="line">  <span class="comment">//禁用prepared statements的仿真效果</span></span><br><span class="line"><span class="comment">//        $dbh-&gt;exec    ("set names 'utf8'");</span></span><br><span class="line">  $sql=<span class="string">"select * from test where user=? and pass=?"</span>;</span><br><span class="line">  $stmt = $dbh-&gt;prepare($sql);</span><br><span class="line">  $exeres = $stmt-&gt;execute(array($user, $pass));</span><br><span class="line">  <span class="keyword">if</span> ($exeres) &#123;</span><br><span class="line">  <span class="comment">//while条件为真时,输出$row,</span></span><br><span class="line">  <span class="keyword">while</span></span><br><span class="line">  ($row = $stmt-&gt;fetch(\PDO::FETCH_ASSOC))&#123;</span><br><span class="line">  print_r($row);die();</span><br><span class="line"> &#125;  <span class="comment">//失败输出登录失败https://learnku.com/articles/27000</span></span><br><span class="line">  print_r(<span class="string">"登录失败"</span>);die();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="二进制上传图片"><a href="#二进制上传图片" class="headerlink" title="二进制上传图片"></a>二进制上传图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"> $postData=file_get_contents(<span class="string">'C:\Users\ASUS\Pictures\Saved Pictures\2.jpg'</span>);</span><br><span class="line">        $curl = curl_init();</span><br><span class="line">        curl_setopt($curl, CURLOPT_URL, <span class="string">'http://www.think.hk/upload1'</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_USERAGENT,<span class="string">'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, <span class="literal">false</span>); <span class="comment">// stop verifying certificate</span></span><br><span class="line">        curl_setopt($curl, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_POST, <span class="literal">true</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_HTTPHEADER, array(<span class="string">'Content-Type: application/x-www-form-urlencoded'</span>));<span class="comment">//注意加这行代码</span></span><br><span class="line">        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);</span><br><span class="line">        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, <span class="literal">true</span>);</span><br><span class="line">        $r = curl_exec($curl);</span><br><span class="line">        curl_close($curl);</span><br><span class="line">        print_r($r);</span><br><span class="line">接收二进制</span><br><span class="line"></span><br><span class="line"> $xmlstr = file_get_contents(<span class="string">'php://input'</span>) ? file_get_contents(<span class="string">'php://input'</span>) : gzuncompress($GLOBALS[<span class="string">'HTTP_RAW_POST_DATA'</span>]);<span class="comment">//得到post过来的二进制原始数据</span></span><br><span class="line"><span class="comment">//echo $xmlstr;die;</span></span><br><span class="line">        $filename=time().<span class="string">'.png'</span>;</span><br><span class="line">        $info=file_put_contents($filename,$xmlstr);</span><br><span class="line">        <span class="keyword">if</span> ($info) &#123;</span><br><span class="line">            $result = [</span><br><span class="line">                <span class="string">'error'</span> =&gt; <span class="string">'成功'</span>,</span><br><span class="line">                <span class="string">'url'</span>   =&gt; $filename</span><br><span class="line">            ];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $result = [<span class="string">'error'</span>   =&gt; <span class="number">1</span>,</span><br><span class="line">                <span class="string">'message'</span> =&gt; <span class="number">404</span></span><br><span class="line">            ];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> json_encode($result);</span><br></pre></td></tr></table></figure>
<h3 id="order-by-进行盲注"><a href="#order-by-进行盲注" class="headerlink" title="order by 进行盲注"></a>order by 进行盲注</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">$username = $_POST[<span class="string">'username'</span>];</span><br><span class="line">$password = $_POST[<span class="string">'password'</span>];</span><br><span class="line"><span class="keyword">if</span>(filter($username))&#123;</span><br><span class="line">    <span class="comment">//过滤括号</span></span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    $sql=<span class="string">"SELECT * FROM admin WHERE username='"</span>.$username.<span class="string">"'"</span>;</span><br><span class="line">    $result=mysql_query($sql);</span><br><span class="line">    @$row = mysql_fetch_array($result);</span><br><span class="line">    <span class="keyword">if</span>(isset($row) &amp;&amp; $row[<span class="string">'username'</span>] === <span class="string">'admin'</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span> ($row[<span class="string">'password'</span>]===md5($password))&#123;</span><br><span class="line">            <span class="comment">//Login successful</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            die(<span class="string">"password error!"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        die(<span class="string">"username does not exist!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">有下列表：</span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">'admin'</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | admin    | <span class="number">51</span>b7a76d51e70b419f60d3473fb6f900 |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">1</span> row <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line">这样一个一般的场景，用户登录时，用户名错误提示：用户名错误，用户名正确密码错误提示：密码错误</span><br><span class="line">username=<span class="string">' union select 1,'</span>admin<span class="string">','</span>c4ca4238a0b923820dcc509a6f75849b&amp;password=<span class="number">1</span></span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">''</span> union select <span class="number">1</span>,<span class="string">'admin'</span>,<span class="string">'c4ca4238a0b923820dcc509a6f75849b'</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | admin    | c4ca4238a0b923820dcc509a6f75849b |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">1</span> row <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line">但是想得到password怎么办</span><br><span class="line"><span class="number">0x03</span> 利用order by起飞</span><br><span class="line">由登录提示可获取一个bool条件，如何用order by利用这个bool条件</span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">''</span> or <span class="number">1</span> union select <span class="number">1</span>,<span class="number">2</span>,<span class="string">'5'</span> order by <span class="number">3</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | <span class="number">2</span>        | <span class="number">5</span>                                |</span><br><span class="line">|  <span class="number">1</span> | admin    | <span class="number">51</span>b7a76d51e70b419f60d3473fb6f900 |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">2</span> rows <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">''</span> or <span class="number">1</span> union select <span class="number">1</span>,<span class="number">2</span>,<span class="string">'6'</span> order by <span class="number">3</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | admin    | <span class="number">51</span>b7a76d51e70b419f60d3473fb6f900 |</span><br><span class="line">|  <span class="number">1</span> | <span class="number">2</span>        | <span class="number">6</span>                                |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">2</span> rows <span class="keyword">in</span> set (<span class="number">0.01</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">''</span> or <span class="number">1</span> union select <span class="number">1</span>,<span class="number">2</span>,<span class="string">'51'</span> order by <span class="number">3</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | <span class="number">2</span>        | <span class="number">51</span>                               |</span><br><span class="line">|  <span class="number">1</span> | admin    | <span class="number">51</span>b7a76d51e70b419f60d3473fb6f900 |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">2</span> rows <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * <span class="keyword">from</span> admin where username=<span class="string">''</span> or <span class="number">1</span> union select <span class="number">1</span>,<span class="number">2</span>,<span class="string">'52'</span> order by <span class="number">3</span>;</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">| id | username | password                         |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line">|  <span class="number">1</span> | admin    | <span class="number">51</span>b7a76d51e70b419f60d3473fb6f900 |</span><br><span class="line">|  <span class="number">1</span> | <span class="number">2</span>        | <span class="number">52</span>                               |</span><br><span class="line">+----+----------+----------------------------------+</span><br><span class="line"><span class="number">2</span> rows <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line">通过逐位判断便可得到password</span><br><span class="line">显然此方法在实际中使用的不多，但在一些特定的环境中也许会用到，比如实验环境，如果过滤了括号，其他盲注基本上就是废了，便可利用order by进行注入。</span><br><span class="line">https:<span class="comment">//p0sec.net/index.php/archives/106/</span></span><br></pre></td></tr></table></figure>
<h3 id="gbk宽字节注入"><a href="#gbk宽字节注入" class="headerlink" title="gbk宽字节注入"></a>gbk宽字节注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$conn = mysql_connect(<span class="string">'127.0.0.1'</span>,<span class="string">'root'</span>,<span class="string">'xxx'</span>); </span><br><span class="line">mysql_select_db(<span class="string">'test'</span>,$conn);</span><br><span class="line">mysql_query(<span class="string">"set names gbk"</span>); </span><br><span class="line">$id = addslashes($_GET[<span class="string">'sql'</span>]); </span><br><span class="line">$sql = <span class="string">"SELECT username,password FROM admin WHERE id='&#123;$id&#125;'"</span>; </span><br><span class="line">echo $sql.<span class="string">'&lt;/br&gt;'</span>;</span><br><span class="line"><span class="keyword">if</span>($res = mysql_query($sql))&#123; </span><br><span class="line"><span class="keyword">while</span>($row = mysql_fetch_array($res))&#123;</span><br><span class="line"> var_dump($row);</span><br><span class="line">&#125;</span><br><span class="line">&#125;<span class="keyword">else</span>&#123; </span><br><span class="line">echo <span class="string">"Error"</span>.mysql_error().<span class="string">"&lt;/br&gt;"</span>; </span><br><span class="line"> &#125; </span><br><span class="line"> $_GET[‘id’]经过addslashes编码之后带入了‘’</span><br><span class="line"> 变成 &lt;pre&gt;<span class="number">2</span>%df%<span class="number">5</span>C%<span class="number">27</span> and updatexml(<span class="number">1</span>,concat(<span class="number">0x7e</span>,(SELECT database()),<span class="number">0x7e</span>),<span class="number">1</span>)%<span class="number">23</span>&lt;<span class="regexp">/pre&gt;</span></span><br><span class="line"><span class="regexp"> 带入mysql处理时使用了gbk字符集</span></span><br><span class="line"><span class="regexp"> %df%5c 運成功的吃掉了%5c</span></span><br><span class="line"><span class="regexp"> %27‘单引号成功闭合</span></span><br><span class="line"><span class="regexp"> GBK编码，编码范围是0x8140~0xFEFE（不包括xx7F），在遇到%df(ascii(223)) &gt;ascii(128)时自动拼接%5c，因此吃掉‘’，而%27、%20小于ascii(128)的字符就保留了https:/</span><span class="regexp">/p0sec.net/i</span>ndex.php/archives/<span class="number">7</span>/</span><br></pre></td></tr></table></figure>
<h3 id="Composer-加速"><a href="#Composer-加速" class="headerlink" title="Composer 加速"></a>Composer 加速</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">composer global <span class="built_in">require</span> hirak/prestissimo</span><br><span class="line">https:<span class="comment">//learnku.com/courses/laravel-package/hirakprestissimo/1695</span></span><br><span class="line"></span><br><span class="line">正常使用 Composer 的相关命令即可，会自动生效。</span><br></pre></td></tr></table></figure>
<h3 id="文件下载乱码"><a href="#文件下载乱码" class="headerlink" title="文件下载乱码"></a>文件下载乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://www.helingfeng.com/2018-05-02/php-file-download-garbled/</span></span><br><span class="line">$encoded_filename = urlencode($filename);</span><br><span class="line">$encoded_filename = str_replace(<span class="string">"+"</span>, <span class="string">"%20"</span>, $encoded_filename);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (preg_match(<span class="string">"/MSIE/"</span>, request()-&gt;userAgent())) &#123;</span><br><span class="line">	header(<span class="string">'Content-Disposition: attachment; filename="'</span> . $encoded_filename . <span class="string">'"'</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (preg_match(<span class="string">"/Firefox/"</span>, request()-&gt;userAgent())) &#123;</span><br><span class="line">	header(<span class="string">'Content-Disposition: attachment; filename*="utf8\'\''</span> . $filename . <span class="string">'"'</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	header(<span class="string">'Content-Disposition: attachment; filename="'</span> . $filename . <span class="string">'"'</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="直接使用-mysql-utf8-存储-超过三个字节的-emoji-表情"><a href="#直接使用-mysql-utf8-存储-超过三个字节的-emoji-表情" class="headerlink" title="直接使用 mysql utf8 存储 超过三个字节的 emoji 表情"></a>直接使用 mysql utf8 存储 超过三个字节的 emoji 表情</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">由于现在数据的版本是<span class="number">5.5</span><span class="number">.2</span>，但是看网上说要直接存储emoji表情，需要升级到<span class="number">5.5</span><span class="number">.3</span>然后把字符集设置为utf8mb4，但是升级数据库感觉属于敏感操作</span><br><span class="line">直接找到接受到的文本中的字符串中<span class="number">4</span>个字节的内容，做rawurlencode编码，再入库。输出的时候再替换回来，然后再用rawurldecode转码回unicode到手机再解析为emoji表情。</span><br><span class="line"></span><br><span class="line">获取字符串长度mb_strlen，按字符来截取字符串mb_substr（mb_strcut是按字节来截取，有区别）。</span><br><span class="line">$strEncode = <span class="string">''</span>;</span><br><span class="line"> </span><br><span class="line">$length = mb_strlen($str,<span class="string">'utf-8'</span>);</span><br><span class="line"> </span><br><span class="line"><span class="keyword">for</span> ($i=<span class="number">0</span>; $i &lt; $length; $i++) &#123;</span><br><span class="line">	$_tmpStr = mb_substr($str,$i,<span class="number">1</span>,<span class="string">'utf-8'</span>);</span><br><span class="line">	<span class="keyword">if</span>(strlen($_tmpStr) &gt;= <span class="number">4</span>)&#123;</span><br><span class="line">		$strEncode .= <span class="string">'[[EMOJI:'</span>.rawurlencode($_tmpStr).<span class="string">']]'</span>;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		$strEncode .= $_tmpStr;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">echo $strEncode.<span class="string">"\n"</span>;<span class="comment">// 周梦康123~☺[[EMOJI:%F0%9F%98%81]][[EMOJI:%F0%9F%98%84]]</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//转码回去</span></span><br><span class="line">$strDecode = preg_replace_callback(<span class="string">"/\[\[EMOJI:(.*?)\]\]/"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$matches</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> rawurldecode($matches[<span class="number">1</span>]);</span><br><span class="line">&#125;, $strEncode);</span><br><span class="line"> </span><br><span class="line">echo $strDecode.<span class="string">"\n"</span>;</span><br><span class="line">第一个坑：我们线上服务器strlen返回的结果不是字节数，而是字符数。我折腾好半天。最后终于找到了原因：</span><br><span class="line"></span><br><span class="line">http:<span class="comment">//php.net/manual/zh/mbstring.overload.php</span></span><br><span class="line"></span><br><span class="line">再配置文件里设置了mbstring.func_overload=<span class="number">2</span>，而且该配置只能在php.ini里修改，不能通过ini_set来改变。具体参考：http:<span class="comment">//php.net/manual/en/mbstring.configuration.php</span></span><br><span class="line">; overload(replace) single byte functions by mbstring functions.</span><br><span class="line">; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),</span><br><span class="line">; etc. Possible values are <span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span> or combination <span class="keyword">of</span> them.</span><br><span class="line">; For example, <span class="number">7</span> <span class="keyword">for</span> overload everything.</span><br><span class="line">; <span class="number">0</span>: No overload</span><br><span class="line">; <span class="number">1</span>: Overload mail() <span class="function"><span class="keyword">function</span></span></span><br><span class="line"><span class="function">; 2: <span class="title">Overload</span> <span class="title">str</span>*(<span class="params"></span>) <span class="title">functions</span></span></span><br><span class="line"><span class="function">; 4: <span class="title">Overload</span> <span class="title">ereg</span>*(<span class="params"></span>) <span class="title">functions</span></span></span><br><span class="line"><span class="function">; <span class="title">http</span>://<span class="title">php</span>.<span class="title">net</span>/<span class="title">mbstring</span>.<span class="title">func</span>-<span class="title">overload</span></span></span><br><span class="line"><span class="function"><span class="title">mbstring</span>.<span class="title">func_overload</span> = 2</span></span><br><span class="line"><span class="function">这样就导致了<span class="title">str</span>*类的函数都被<span class="title">mb_str</span>*给替换了。具体的参看上面的链接。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">第二个坑：<span class="title">PHP</span> 5.3.13中对<span class="title">preg_replace_callback</span>不支持 <span class="title">https</span>://<span class="title">mengkang</span>.<span class="title">net</span>/384.<span class="title">html</span></span></span><br></pre></td></tr></table></figure>
<h3 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/31829</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Elem</span></span>&#123;</span><br><span class="line">    public $key;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Table</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $data = [];</span><br><span class="line">    public $lenght;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createTable</span>(<span class="params">Table &amp;$table,$length</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $table-&gt;lenght = $length;</span><br><span class="line">    $obj = <span class="keyword">new</span> Elem();</span><br><span class="line">    fwrite(STDOUT,<span class="string">"请输入数据：\n"</span>);</span><br><span class="line">    <span class="keyword">for</span>($i=<span class="number">1</span>;$i&lt;=$table-&gt;lenght;$i++)&#123;</span><br><span class="line">        $elem = clone $obj;</span><br><span class="line">        $elem-&gt;key = fgets(STDIN,<span class="number">10</span>);</span><br><span class="line">        $table-&gt;data[$i] = $elem;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">searchBin</span>(<span class="params">Table $table,$key</span>):<span class="title">int</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $low = <span class="number">1</span>;</span><br><span class="line">    $height = $table-&gt;lenght;</span><br><span class="line">    <span class="keyword">while</span>($low&lt;=$height)&#123;</span><br><span class="line">        $mid = floor(($low+intval($height))/<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">if</span> ($table-&gt;data[$mid]-&gt;key==$key)&#123;</span><br><span class="line">            <span class="keyword">return</span> $mid;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>($table-&gt;data[$mid]-&gt;key&gt;$key)&#123;</span><br><span class="line">            $height=$mid<span class="number">-1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            $low = $mid+<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $table = <span class="keyword">new</span> Table();</span><br><span class="line">    fwrite(STDOUT,<span class="string">"请输入数据元素个数：\n"</span>);</span><br><span class="line">    $length = fgets(STDIN,<span class="number">10</span>);</span><br><span class="line">    createTable($table,$length);</span><br><span class="line">    fwrite(STDOUT,<span class="string">"请输入要查找的数据元素：\n"</span>);</span><br><span class="line">    $key = (integer)fgets(STDIN,<span class="number">10</span>);</span><br><span class="line">    $location = searchBin($table,$key);</span><br><span class="line">    <span class="keyword">if</span> ($location)&#123;</span><br><span class="line">        fwrite(STDOUT,<span class="string">"查找到的数据索引为：$location\n"</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        fwrite(STDERR,<span class="string">"查找不到指定的内容\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<h3 id="无限分类"><a href="#无限分类" class="headerlink" title="无限分类"></a>无限分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br></pre></td><td class="code"><pre><span class="line">$arrs = [</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">1</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">2</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">2</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">5</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">];</span><br><span class="line">var_export(getTree($arrs));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getTree</span>(<span class="params">$arrs,$root=<span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $tree = array();</span><br><span class="line">    foreach ($arrs <span class="keyword">as</span> $foo) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($foo[<span class="string">'parent_id'</span>] == $root) &#123;</span><br><span class="line">            $foo[<span class="string">'children'</span>] = getTree($arrs,$foo[<span class="string">'id'</span>]);</span><br><span class="line">            $tree[] = $foo;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getTree</span>(<span class="params">$arrs,$root=<span class="number">0</span>,$level=<span class="number">100</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $tree = array();</span><br><span class="line">    foreach ($arrs <span class="keyword">as</span> $foo) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($foo[<span class="string">'parent_id'</span>] == $root) &#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">if</span>($level&gt;<span class="number">0</span>)&#123;               </span><br><span class="line">            $foo[<span class="string">'children'</span>] = getTree($arrs,$foo[<span class="string">'id'</span>],$level<span class="number">-1</span>);     </span><br><span class="line">           &#125;</span><br><span class="line">            $tree[] = $foo;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    --$level;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 把返回的数据集转换成Tree</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param $list</span></span><br><span class="line"><span class="comment">     * @param string $pk</span></span><br><span class="line"><span class="comment">     * @param string $pid</span></span><br><span class="line"><span class="comment">     * @param string $child</span></span><br><span class="line"><span class="comment">     * @param int $root</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">list_to_tree</span>(<span class="params">$list, $pk = <span class="string">'id'</span>, $pid = <span class="string">'pid'</span>, $child = <span class="string">'_child'</span>, $root = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 创建Tree</span></span><br><span class="line">        $tree = [];</span><br><span class="line">        <span class="keyword">if</span> (is_array($list)) &#123;</span><br><span class="line">            <span class="comment">// 创建基于主键的数组引用</span></span><br><span class="line">            $refer = [];</span><br><span class="line">            foreach ($list <span class="keyword">as</span> $key =&gt; $data) &#123;</span><br><span class="line">                $refer[$data[$pk]] = &amp;$list[$key];</span><br><span class="line">            &#125;</span><br><span class="line">            foreach ($list <span class="keyword">as</span> $key =&gt; $data) &#123;</span><br><span class="line">                <span class="comment">// 判断是否存在parent</span></span><br><span class="line">                $parentId = $data[$pid];</span><br><span class="line">                <span class="keyword">if</span> ($root == $parentId) &#123;</span><br><span class="line">                    $tree[] = &amp;$list[$key];</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (isset($refer[$parentId])) &#123;</span><br><span class="line">                        $parent = &amp;$refer[$parentId];</span><br><span class="line">                        $parent[$child][] = &amp;$list[$key];</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        $tree[] = &amp;$list[$key];</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $tree;</span><br><span class="line">    &#125;</span><br><span class="line">    使用模型自带关联关系可以实现无限级</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 返回子节点</span></span><br><span class="line"><span class="comment">         * @return \Illuminate\Database\Eloquent\Relations\HasMany</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>),<span class="string">'p_id'</span>,<span class="string">'id'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 所有子节点https://learnku.com/laravel/t/31828</span></span><br><span class="line"><span class="comment">         * @return \Illuminate\Database\Eloquent\Relations\HasMany</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">allChildren</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;children()-&gt;<span class="keyword">with</span>( <span class="string">'allChildren'</span> );</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 父节点</span></span><br><span class="line"><span class="comment">         * @return \Illuminate\Database\Eloquent\Relations\HasOne</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">parent</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasOne(get_class($<span class="keyword">this</span>),<span class="string">'id'</span>,<span class="string">'p_id'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        https:<span class="comment">//github.com/lazychaser/laravel-nestedset</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getTree</span>(<span class="params">$arrs,$root=<span class="number">0</span>,$level=<span class="number">100</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $tree = array();</span><br><span class="line">    foreach ($arrs <span class="keyword">as</span> $foo) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($foo[<span class="string">'parent_id'</span>] == $root) &#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">if</span>($level&gt;<span class="number">0</span>)&#123;               </span><br><span class="line">            $foo[<span class="string">'children'</span>] = getTree($arrs,$foo[<span class="string">'id'</span>],$level<span class="number">-1</span>);     </span><br><span class="line">           &#125;</span><br><span class="line">            $tree[] = $foo;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    --$level;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;        </span><br><span class="line">    var_export ($arrs,<span class="number">0</span>,<span class="number">0</span>)<span class="comment">// 一级</span></span><br><span class="line">    </span><br><span class="line">    array (</span><br><span class="line">      <span class="number">0</span> =&gt; </span><br><span class="line">      array (</span><br><span class="line">        <span class="string">'id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">        <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">      ),</span><br><span class="line">      <span class="number">1</span> =&gt; </span><br><span class="line">      array (</span><br><span class="line">        <span class="string">'id'</span> =&gt; <span class="number">5</span>,</span><br><span class="line">        <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">      ),</span><br><span class="line">    )</span><br><span class="line">    var_export ($arrs,<span class="number">0</span>,<span class="number">1</span>)<span class="comment">// 两级</span></span><br><span class="line">    </span><br><span class="line">    array (</span><br><span class="line">      <span class="number">0</span> =&gt; </span><br><span class="line">      array (</span><br><span class="line">        <span class="string">'id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">        <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">        <span class="string">'children'</span> =&gt; </span><br><span class="line">        array (</span><br><span class="line">          <span class="number">0</span> =&gt; </span><br><span class="line">          array (</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">      <span class="number">1</span> =&gt; </span><br><span class="line">      array (</span><br><span class="line">        <span class="string">'id'</span> =&gt; <span class="number">5</span>,</span><br><span class="line">        <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">        <span class="string">'children'</span> =&gt; </span><br><span class="line">        array (</span><br><span class="line">          <span class="number">0</span> =&gt; </span><br><span class="line">          array (</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">6</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">5</span>,</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    )</span><br></pre></td></tr></table></figure>
<h3 id="php加密"><a href="#php加密" class="headerlink" title="php加密"></a>php加密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line">AES 加密 / 解密</span><br><span class="line"></span><br><span class="line"><span class="comment">//获取可用的密码加密算法列表</span></span><br><span class="line"><span class="comment">//$methods = openssl_get_cipher_methods();</span></span><br><span class="line"><span class="comment">//var_dump($methods);</span></span><br><span class="line"></span><br><span class="line"># AES加密演示</span><br><span class="line"><span class="comment">//明文(要加密的内容)</span></span><br><span class="line">$str = <span class="string">"这是测试用例 我是明文"</span>;</span><br><span class="line"><span class="comment">//秘钥(用例:使用uniqid()函数生成了一个唯一ID)</span></span><br><span class="line">$key = <span class="string">"5d3fb4acb2292"</span>;</span><br><span class="line"><span class="comment">//加密算法</span></span><br><span class="line">$method = <span class="string">"AES-128-CBC"</span>;</span><br><span class="line"><span class="comment">//加密向量(要求18个字节)</span></span><br><span class="line">$iv = <span class="string">"1234567812345678"</span>;</span><br><span class="line">$encrypt_str = openssl_encrypt($str, $method, $key, <span class="number">0</span>, $iv);</span><br><span class="line">var_dump(<span class="string">"AES加密结果:"</span>.$encrypt_str);</span><br><span class="line"></span><br><span class="line"># AES解密演示</span><br><span class="line"><span class="comment">//$encrypt_str AES加密后产生的密文</span></span><br><span class="line"><span class="comment">//$key 秘钥(同上)</span></span><br><span class="line">$decrypt_str = openssl_decrypt($encrypt_str, $method, $key, <span class="number">0</span>, $iv);</span><br><span class="line">var_dump(<span class="string">"AES解密结果:"</span>.$decrypt_str);</span><br><span class="line"></span><br><span class="line">RSA 加密</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>公/私钥加密算法,属于非对称加密:</span><br><span class="line"><span class="number">2.</span>优点:极难被破解;</span><br><span class="line"><span class="number">3.</span>缺点:速度慢,运算次数多,不适合加密长文本;</span><br><span class="line"><span class="comment">//公钥(项目中可在线生产亦可自己生成)</span></span><br><span class="line">$PUBLIC_KEY = <span class="string">"-----BEGIN PUBLIC KEY-----</span></span><br><span class="line"><span class="string">MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApJJ7D/U9lHLNQdl4LZSr</span></span><br><span class="line"><span class="string">jNvdCelIraMnSD/iujWxyw/QDLXPCtP06ll42JURGlYaO2DU5c5BKEUF0alyzlE9</span></span><br><span class="line"><span class="string">XiHRXPl0LabI/CjGtrIB4RApy1PjkQ31QOt+9R2Nmb7RUkfZwnCWHBlNVnwj4U6J</span></span><br><span class="line"><span class="string">woccrlUdElBWU5twFc2PNPbMR6nA/ldUwDpcveNHNp57BrgYfUFcLrjmf2LH6c7X</span></span><br><span class="line"><span class="string">ngBNPbG5ha5pmsaXm8MAqBRtAvIwvUsvJLIr+XRc27pCJFe/1MtS4hHhTPE4un/z</span></span><br><span class="line"><span class="string">Y/tIrpqm6MimdJcs8oqEQPoztfs5BTNu2jVgrKwtWExDXODWmHemQoaCwzgt3wMy</span></span><br><span class="line"><span class="string">3wIDAQAB</span></span><br><span class="line"><span class="string">-----END PUBLIC KEY-----"</span>;</span><br><span class="line"><span class="comment">//私钥(项目中可在线生产亦可自己生成)</span></span><br><span class="line">$PRIVATE_KEY = <span class="string">"-----BEGIN PRIVATE KEY-----</span></span><br><span class="line"><span class="string">MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCkknsP9T2Ucs1B</span></span><br><span class="line"><span class="string">2XgtlKuM290J6UitoydIP+K6NbHLD9AMtc8K0/TqWXjYlREaVho7YNTlzkEoRQXR</span></span><br><span class="line"><span class="string">qXLOUT1eIdFc+XQtpsj8KMa2sgHhECnLU+ORDfVA6371HY2ZvtFSR9nCcJYcGU1W</span></span><br><span class="line"><span class="string">fCPhTonChxyuVR0SUFZTm3AVzY809sxHqcD+V1TAOly940c2nnsGuBh9QVwuuOZ/</span></span><br><span class="line"><span class="string">YsfpzteeAE09sbmFrmmaxpebwwCoFG0C8jC9Sy8ksiv5dFzbukIkV7/Uy1LiEeFM</span></span><br><span class="line"><span class="string">8Ti6f/Nj+0iumqboyKZ0lyzyioRA+jO1+zkFM27aNWCsrC1YTENc4NaYd6ZChoLD</span></span><br><span class="line"><span class="string">OC3fAzLfAgMBAAECggEAUcCieW7uREwzQr7xQFNWVQbzavUEMZ2W6gEydCYwSBt2</span></span><br><span class="line"><span class="string">0pmOXGamh7QioBSNBnQ3W7/igrZPD94Z4ek3Kt6YiaZrBrC00ejEdt8at6791/vb</span></span><br><span class="line"><span class="string">hzIJHgm9B5701nbz3Kg5+5HNzxV2vEalcI0Cle4Z6RSNXtzRMEPQXoAc0ffnZ/tV</span></span><br><span class="line"><span class="string">033zAN4nWb9zeLw03/D0nbcpaYA/WbwqsNiTxbbi0s54oTsaOTMBBAK9oH9H2M5J</span></span><br><span class="line"><span class="string">506iINcKniyMi6i0cf/cQ+tP6VUCOMHdWm/zJmQ5s2eU/2SowSKMXMLIGUH2Q4AX</span></span><br><span class="line"><span class="string">Z2htX4YwvdHGlGA5yPuiMznkFidVcERfbVl9yi54YQKBgQDQYqj2bb0bvD8YuvXx</span></span><br><span class="line"><span class="string">htdBQrxiX53pZ1sVoh5SMxD+Lq6tpn4UtOJw6tpE7tgONmWRaKCH10fgX5nQoXPJ</span></span><br><span class="line"><span class="string">0Y02qiDyk/TkE0OGiYRTjjkjY3yPkBIz9KRCoIUcwirEfWdzmjFLTq9hiaGo9JXN</span></span><br><span class="line"><span class="string">HcLXOgpAbiQe+qXf9x/waWB/hQKBgQDKLQB9Ep9A6UFlumXaEr971A7HcQI2BsfP</span></span><br><span class="line"><span class="string">kRfCcT1rphnENHCa37o+5i6tTImAXI+aayp9Jpv0rXLbzFbBkdUdUDINulXSsLRT</span></span><br><span class="line"><span class="string">bq3ttbu5c+NG21XW1fvVqf4VYOP7u/l0Z2eBIsg9uLswS3zltTG8ikm+RKhMf1DV</span></span><br><span class="line"><span class="string">PDAOoLmMEwKBgDn0po9a9/Rlx5qmLM7OtMFGwUQO2clXYILEwvATmc9HxncvTfOO</span></span><br><span class="line"><span class="string">V0gWWTxAvUA+qsLlOXhuTGQ/0nSu4pgnusGQUXeF5N8l6Grbhj0C2itYeQUoiZd/</span></span><br><span class="line"><span class="string">m8uX/01/Rwu84O/K25jZOnfDIn3uAFe6xjy7vKwstckT5txCS9S+SgNNAoGAbvLl</span></span><br><span class="line"><span class="string">Sr32cUvQXMA+9r7FIHJOLfsBaJ6t9mW8cTNtrm63wym4BfXzImN1iBrxdmTVVbur</span></span><br><span class="line"><span class="string">1IRkn5Cz8JUhoxahqnWBEnGIeZgJTaP2hPXvcCV9uzvQzpYdnrKsQhUq59HPYqcA</span></span><br><span class="line"><span class="string">cSiiVOTUrPswLmsSQVJuh6Dr7xcLSAnAobZoPMsCgYEAsJuY5RcB1sjYortRNsKb</span></span><br><span class="line"><span class="string">KHLiLI93P0MFF46V/343d3BU7TZfETg703Mj2AfOAGTM2p2BkHFri3l+4oigMqpr</span></span><br><span class="line"><span class="string">hAp4hNq4KFK2SCjzedrLV7QIgtp/uMZ+q/yhRtiG8kSWlI9c0Un00+KqIwFqfwAB</span></span><br><span class="line"><span class="string">l1zOX5QcMa1X7eWSvZ559ko=</span></span><br><span class="line"><span class="string">-----END PRIVATE KEY-----"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//待加密明文</span></span><br><span class="line">$data = <span class="string">"这是RSA待加密明文"</span>;</span><br><span class="line"><span class="comment">//用于接收加密后的密文</span></span><br><span class="line">$content_encrypt = <span class="string">""</span>;</span><br><span class="line"> # 私钥加密</span><br><span class="line">openssl_private_encrypt($data, $content_encrypt, $PRIVATE_KEY, <span class="number">1</span>);</span><br><span class="line">var_dump(<span class="string">"私钥加密结果:"</span>.$content_encrypt);</span><br><span class="line"></span><br><span class="line"> # 公钥解密https://learnku.com/articles/31841</span><br><span class="line"><span class="comment">//$content_encrypt 私钥加密后的密文</span></span><br><span class="line"><span class="comment">//用于接收解密后的明文</span></span><br><span class="line">$content_decrypt = <span class="string">""</span>;</span><br><span class="line">openssl_public_decrypt($content_encrypt, $content_decrypt, $PUBLIC_KEY, <span class="number">1</span>);</span><br><span class="line">var_dump(<span class="string">"公钥解密结果:"</span>.$content_decrypt);</span><br><span class="line"><span class="comment">//根军 MD5()函数的不可逆性进行签名校验</span></span><br><span class="line"><span class="comment">//首先必须要有 $appKey与$secretKey</span></span><br><span class="line">$appKey = <span class="string">"5d3fb4acb2292"</span>;</span><br><span class="line">$secretKey = <span class="string">"5d3fb4acb22925d3fb4acb22925d3fb4acb2292"</span>;</span><br><span class="line"></span><br><span class="line">$url = <span class="string">"localhost/text_sig.php"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//待传递的参数</span></span><br><span class="line">$params[<span class="string">'appKey'</span>] = $appKey;</span><br><span class="line">$params[<span class="string">'name'</span>] = <span class="string">"张三"</span>;</span><br><span class="line">$params[<span class="string">'age'</span>] = <span class="string">"26"</span>;</span><br><span class="line">$params[<span class="string">'sex'</span>] = <span class="string">"男"</span>;</span><br><span class="line">$params[<span class="string">'root'</span>] = <span class="string">"admin"</span>;</span><br><span class="line">$params[<span class="string">'password'</span>] = <span class="string">"123456"</span>;</span><br><span class="line">$params[<span class="string">'time'</span>] = time();</span><br><span class="line"><span class="comment">//获取签名</span></span><br><span class="line">$params[<span class="string">'sig'</span>] = createSig($params,$secretKey);</span><br><span class="line">$param_str = http_build_query($params);</span><br><span class="line">$url = $url.<span class="string">'?'</span>.$param_str;</span><br><span class="line">var_dump($url);</span><br><span class="line"></span><br><span class="line"><span class="comment">//生成签名</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createSig</span>(<span class="params">$params,$secretKey</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//对参数进行排序</span></span><br><span class="line">    ksort($params);</span><br><span class="line">    $str = http_build_query($params);</span><br><span class="line">    $str .= $secretKey;</span><br><span class="line">    <span class="keyword">return</span> md5($str);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="文字生成点阵图"><a href="#文字生成点阵图" class="headerlink" title="文字生成点阵图"></a>文字生成点阵图</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$font_width = <span class="number">16</span>; <span class="comment">// 单字宽度</span></span><br><span class="line">$font_height = <span class="number">16</span>; <span class="comment">// 单字高度</span></span><br><span class="line">$byteCount = $font_width * $font_height / <span class="number">8</span>;<span class="comment">//一个点阵占的字节数</span></span><br><span class="line"></span><br><span class="line">$str=iconv(<span class="string">"utf-8"</span>,<span class="string">"gb2312//IGNORE"</span>, <span class="string">"我是大是的大萨法fasf大多数"</span>);</span><br><span class="line">$n=<span class="number">5</span>;<span class="comment">//每一行四个字</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//所有字的字模</span></span><br><span class="line">$dot = getDot($str,$byteCount);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * $positions 平面坐标系</span></span><br><span class="line"><span class="comment"> * $sections 每一个字模在平面坐标系中的点。（可移动每一个字模的位置）</span></span><br><span class="line"><span class="comment"> * $spreadSections 所有字模在平面坐标系中的点，是$sections的展开</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">list($positions,$sections,$spreadSections) = getPositionsSections($dot,$byteCount,$n);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 输出点阵html字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">echo getOutHtml($positions,$sections,$spreadSections,$n);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 从字库中获得每一个字的字模</span></span><br><span class="line"><span class="comment"> * @param $str</span></span><br><span class="line"><span class="comment"> * @param $byteCount</span></span><br><span class="line"><span class="comment"> * @return string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getDot</span>(<span class="params">$str,$byteCount</span>)</span>&#123;</span><br><span class="line">    $dot=<span class="string">''</span>;</span><br><span class="line">    $fontFileName = <span class="string">'./HZK16'</span>;<span class="comment">//字库名字</span></span><br><span class="line">    $fp = fopen($fontFileName, <span class="string">"rb"</span>);</span><br><span class="line">    <span class="keyword">for</span> ($i=<span class="number">0</span>;$i&lt;strlen($str);$i++)&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(ord($str[$i])&lt;<span class="number">160</span>)&#123;<span class="comment">//非汉字</span></span><br><span class="line">            $location=(ord($str&#123;$i&#125;) + <span class="number">156</span><span class="number">-1</span>) * $byteCount;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;<span class="comment">//汉字</span></span><br><span class="line">            $qh = ord($str[$i]) - <span class="number">32</span> - <span class="number">128</span>;<span class="comment">//区码</span></span><br><span class="line">            $wh = ord($str[++$i]) - <span class="number">32</span> - <span class="number">128</span>;<span class="comment">//位码</span></span><br><span class="line">            $location = (<span class="number">94</span> * ($qh - <span class="number">1</span>) + ($wh - <span class="number">1</span>)) * $byteCount; <span class="comment">/* 计算汉字字模在文件中的位置 */</span></span><br><span class="line">        &#125;</span><br><span class="line">        fseek($fp, $location, SEEK_SET);<span class="comment">//定位到汉字或字母指针开始的地方</span></span><br><span class="line">        $dot.= fread($fp, $byteCount);<span class="comment">//读取32字节的长度，一个字节8位，一行依次放两个字节组成16*16的点阵</span></span><br><span class="line">    &#125;</span><br><span class="line">    fclose($fp);</span><br><span class="line">    <span class="keyword">return</span> $dot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 建平面按坐标系。并把每一区块用平面坐标系表示</span></span><br><span class="line"><span class="comment"> * @param $dot</span></span><br><span class="line"><span class="comment"> * @param $byteCount</span></span><br><span class="line"><span class="comment"> * @param $n</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPositionsSections</span>(<span class="params">$dot,$byteCount,$n</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    $count= strlen($dot)/$byteCount;<span class="comment">//多少个字</span></span><br><span class="line">    $positions=[];</span><br><span class="line">    $sections =[];</span><br><span class="line">    $sectionCount=$count;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> ($i=<span class="number">0</span>;$i&lt;$sectionCount;$i++)&#123;</span><br><span class="line">        $sections[]=[];</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $yHeight=(intval($count/$n)*<span class="number">16</span>+<span class="number">16</span>);</span><br><span class="line">    $xWeight=<span class="number">16</span>*$n;</span><br><span class="line">    <span class="keyword">for</span> ($i=<span class="number">0</span>;$i&lt;$yHeight;$i++)&#123;</span><br><span class="line">        <span class="keyword">for</span> ($j=<span class="number">0</span>;$j&lt;$xWeight;$j++)&#123;</span><br><span class="line">            $positions []=[$j,$i];</span><br><span class="line">            $x=ceil(($j+<span class="number">1</span>)/<span class="number">16</span>);</span><br><span class="line">            $y=ceil(($i+<span class="number">1</span>)/<span class="number">16</span>);</span><br><span class="line">            $y--;</span><br><span class="line">            $x--;</span><br><span class="line">            $sections[(($y)*$n+$x)][] = [$j,$i];</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> ($b=<span class="number">0</span>;$b&lt;$count;$b++)&#123;<span class="comment">//每一个字占用的点阵</span></span><br><span class="line">        $str = substr($dot,($b)*<span class="number">32</span>,$byteCount);<span class="comment">//第几个字</span></span><br><span class="line">        $dot_string=<span class="string">''</span>;</span><br><span class="line">        <span class="keyword">for</span> ($c = <span class="number">0</span>; $c &lt; $byteCount; $c++)&#123;</span><br><span class="line">            $dot_string .= sprintf(<span class="string">"%08b"</span>, ord($str[$c]));</span><br><span class="line">            <span class="keyword">if</span> ($c % <span class="number">2</span> == <span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">for</span>($a=<span class="number">0</span>;$a&lt;strlen($dot_string);$a++)&#123;</span><br><span class="line">                    <span class="keyword">if</span>($dot_string[$a])&#123;<span class="comment">//和平面坐标系关联起来</span></span><br><span class="line">                        $sections[$b][intval(<span class="number">16</span>*floor($c/<span class="number">2</span>)+$a)][]=<span class="number">1</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                $dot_string = <span class="string">''</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    $spreadSections=[];<span class="comment">//每一个字块的的点展开到数组中</span></span><br><span class="line">    foreach ($sections <span class="keyword">as</span> $section)&#123;</span><br><span class="line">        $spreadSections  = array_merge($spreadSections,$section);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> [$positions,$sections,$spreadSections,$count,$sectionCount];</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getOutHtml</span>(<span class="params">$positions,$sections,$spreadSections,$n</span>)</span>&#123;</span><br><span class="line">    $str=<span class="string">"&lt;html&gt;&lt;body&gt;&lt;table border='1' width='100%' style='text-align: center'&gt;"</span>;</span><br><span class="line">    foreach (array_chunk($positions,<span class="number">16</span>*$n) <span class="keyword">as</span> $row)&#123;</span><br><span class="line"></span><br><span class="line">        $str.=getOutRow($row,$sections,$spreadSections);</span><br><span class="line">    &#125;</span><br><span class="line">    $str .= <span class="string">"&lt;/table&gt;&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;"</span>;</span><br><span class="line">    <span class="keyword">return</span> $str;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getOutRow</span>(<span class="params">$row,$sections,$spreadSections</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    $str=<span class="string">"&lt;tr&gt;"</span>;</span><br><span class="line">    foreach ($row <span class="keyword">as</span> $td) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!in_array($td,$spreadSections))&#123;<span class="comment">//不在平面坐标系中说明这个位置是一个点</span></span><br><span class="line">            $str .= <span class="string">"&lt;td style='color: white;background-color: red;'&gt;O&lt;/td&gt;"</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            $str .= <span class="string">"&lt;td&gt;O&lt;/td&gt;"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    $str.=<span class="string">"&lt;tr&gt;"</span>;</span><br><span class="line">    <span class="keyword">return</span> $str;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/31882</span></span><br></pre></td></tr></table></figure>
<h3 id="Guzzle-中间件进行优雅的请求重试"><a href="#Guzzle-中间件进行优雅的请求重试" class="headerlink" title="Guzzle 中间件进行优雅的请求重试"></a>Guzzle 中间件进行优雅的请求重试</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">use GuzzleHttp\Psr7\Request;</span><br><span class="line">use GuzzleHttp\Psr7\Response;</span><br><span class="line">use GuzzleHttp\Client;</span><br><span class="line">use GuzzleHttp\Exception\ConnectException;</span><br><span class="line">use GuzzleHttp\Exception\RequestException;</span><br><span class="line">use GuzzleHttp\Handler\CurlHandler;</span><br><span class="line">use GuzzleHttp\HandlerStack;</span><br><span class="line">use GuzzleHttp\Middleware;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestGuzzleRetry</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 最大重试次数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">const</span> MAX_RETRIES = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @var Client</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $client;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * GuzzleRetry constructor.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 创建 Handler</span></span><br><span class="line">        $handlerStack = HandlerStack::create(<span class="keyword">new</span> CurlHandler());</span><br><span class="line">        <span class="comment">// 创建重试中间件，指定决策者为 $this-&gt;retryDecider(),指定重试延迟为 $this-&gt;retryDelay()</span></span><br><span class="line">        $handlerStack-&gt;push(Middleware::retry($<span class="keyword">this</span>-&gt;retryDecider(), $<span class="keyword">this</span>-&gt;retryDelay()));</span><br><span class="line">        <span class="comment">// 指定 handler</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;client = <span class="keyword">new</span> Client([<span class="string">'handler'</span> =&gt; $handlerStack]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * retryDecider</span></span><br><span class="line"><span class="comment">     * 返回一个匿名函数, 匿名函数若返回false 表示不重试，反之则表示继续重试</span></span><br><span class="line"><span class="comment">     * @return Closure</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">retryDecider</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">            $retries,</span></span></span><br><span class="line"><span class="function"><span class="params">            Request $request,</span></span></span><br><span class="line"><span class="function"><span class="params">            Response $response = null,</span></span></span><br><span class="line"><span class="function"><span class="params">            RequestException $exception = null</span></span></span><br><span class="line"><span class="function"><span class="params">        </span>) </span>&#123;</span><br><span class="line">            <span class="comment">// 超过最大重试次数，不再重试</span></span><br><span class="line">            <span class="keyword">if</span> ($retries &gt;= self::MAX_RETRIES) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 请求失败，继续重试</span></span><br><span class="line">            <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> ConnectException) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> ($response) &#123;</span><br><span class="line">                <span class="comment">// 如果请求有响应，但是状态码大于等于500，继续重试(这里根据自己的业务而定)</span></span><br><span class="line">                <span class="keyword">if</span> ($response-&gt;getStatusCode() &gt;= <span class="number">500</span>) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回一个匿名函数，该匿名函数返回下次重试的时间（毫秒）https://learnku.com/articles/31855</span></span><br><span class="line"><span class="comment">     * @return Closure</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">retryDelay</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">$numberOfRetries</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1000</span> * $numberOfRetries;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二进制运算符按位与-amp"><a href="#二进制运算符按位与-amp" class="headerlink" title="二进制运算符按位与 &amp;"></a>二进制运算符按位与 &amp;</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">爱好： 篮球 ☑ 足球 ☑ 羽毛球 ☑</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">在数据库中的存储有一下 <span class="number">2</span> 种思路：</span><br><span class="line"></span><br><span class="line"><span class="number">1</span> 定义一个字段 hobby tinyint <span class="number">1</span> 篮球 <span class="number">2</span> 足球 <span class="number">3</span> 羽毛球 <span class="number">4</span> 篮球 + 足球 <span class="number">5</span> 篮球 + 羽毛球 <span class="number">6</span> 篮球 + 羽毛球 <span class="number">7</span> 足球 + 羽毛球 <span class="number">8</span> 篮球 + 足球 + 羽毛球</span><br><span class="line"></span><br><span class="line">即：把这三个选项进行排列组合找出每种情况进行存储</span><br><span class="line"></span><br><span class="line">这样做可以满足业务的需求，但是如果要是将来再添加一个选项的话，那么将是一场灾难。</span><br><span class="line"></span><br><span class="line"><span class="number">2</span> 使用 php 中的 &amp; 运算符 按位与运算符</span><br><span class="line"></span><br><span class="line">定义如下</span><br><span class="line"></span><br><span class="line"><span class="number">2</span> 篮球 <span class="number">4</span> 足球 <span class="number">8</span> 羽毛球</span><br><span class="line"></span><br><span class="line">数据库中的 hobby 字段存储的就是选择后的和，这样在进行编辑或者获取这个用户的爱好的时候只要做一次按位与 &amp; 运算就行了</span><br><span class="line"></span><br><span class="line">例子：</span><br><span class="line"></span><br><span class="line">如果我选择了篮球 足球 乒乓球 则数据库存的是所有之和 <span class="number">14</span> 在编辑的时候只需要从数据库里的 <span class="number">14</span> 和选项做与 &amp; 运算 大于 <span class="number">0</span> 说明选择了 小于 <span class="number">0</span> 则说明没有选择 $res = <span class="number">14</span> &amp; $pingpong; 输出 <span class="number">8</span> var_dump ($res); https:<span class="comment">//learnku.com/articles/31874</span></span><br></pre></td></tr></table></figure>
<h3 id="Composer-安装插件swap-内存不足"><a href="#Composer-安装插件swap-内存不足" class="headerlink" title="Composer 安装插件swap 内存不足"></a>Composer 安装插件swap 内存不足</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/laravel/t/27985</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">/bin/dd <span class="keyword">if</span>=<span class="regexp">/dev/</span>zero <span class="keyword">of</span>=<span class="regexp">/var/</span>swap<span class="number">.1</span> bs=<span class="number">1</span>M count=<span class="number">2048</span></span><br><span class="line"></span><br><span class="line"> /sbin/mkswap /<span class="keyword">var</span>/swap<span class="number">.1</span></span><br><span class="line"></span><br><span class="line"> /sbin/swapon /<span class="keyword">var</span>/swap<span class="number">.1</span></span><br><span class="line"> </span><br><span class="line"> free -m</span><br><span class="line"> mkdir -p /<span class="keyword">var</span>/_swap_</span><br><span class="line"> cd /<span class="keyword">var</span>/_swap_</span><br><span class="line"> # Here, 1M * 2000 ~= 2GB of swap memory</span><br><span class="line"> dd <span class="keyword">if</span>=<span class="regexp">/dev/</span>zero <span class="keyword">of</span>=swapfile bs=<span class="number">1</span>M count=<span class="number">2000</span></span><br><span class="line"> mkswap swapfile</span><br><span class="line"> swapon swapfile</span><br><span class="line"> chmod <span class="number">600</span> swapfile</span><br><span class="line"> echo <span class="string">"/var/_swap_/swapfile none swap sw 0 0"</span> &gt;&gt; <span class="regexp">/etc/</span>fstab</span><br><span class="line"> cat /proc/meminfo</span><br><span class="line"> free -m</span><br></pre></td></tr></table></figure>
<h3 id="crc32-检错数据"><a href="#crc32-检错数据" class="headerlink" title="crc32 检错数据"></a>crc32 检错数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">$checksum = crc32(<span class="string">"The quick brown fox jumped over the lazy dog."</span>);</span><br><span class="line">printf(<span class="string">"%u\n"</span>, $checksum);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//32位和64位支持</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">crcKw</span>(<span class="params">$num</span>)</span>&#123; </span><br><span class="line">    $crc = crc32($num); </span><br><span class="line">    <span class="keyword">if</span>($crc &amp; <span class="number">0x80000000</span>)&#123; </span><br><span class="line">        $crc ^= <span class="number">0xffffffff</span>; </span><br><span class="line">        $crc += <span class="number">1</span>; </span><br><span class="line">        $crc = -$crc; </span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">return</span> $crc; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">var_export(crcKw($checksum));</span><br><span class="line"><span class="number">2191738434</span></span><br><span class="line"><span class="number">512553366</span></span><br></pre></td></tr></table></figure>
<h3 id="下载带有换行符的文件内容"><a href="#下载带有换行符的文件内容" class="headerlink" title="下载带有换行符的文件内容"></a>下载带有换行符的文件内容</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$lists=<span class="string">'tFwdQlk3Xs,mSL6m9P19Y,2sF2r7MxBh'</span>;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//要用双引号</span></span><br><span class="line">  $string=implode(<span class="string">"\r\n\r\n"</span>,explode(<span class="string">','</span>,$lists) ) ;</span><br><span class="line">  $filename=<span class="string">'test.txt'</span>;</span><br><span class="line">  header(<span class="string">"Content-Transfer-Encoding: binary"</span>);</span><br><span class="line">  header(<span class="string">'Content-disposition: attachment; filename='</span>.$filename);</span><br><span class="line"> </span><br><span class="line">  echo $string;</span><br></pre></td></tr></table></figure>
<h3 id="两个字符串的相似度"><a href="#两个字符串的相似度" class="headerlink" title="两个字符串的相似度"></a>两个字符串的相似度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输入拼写错误的单词</span></span><br><span class="line">$input = <span class="string">'carrrot'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 要检查的单词数组</span></span><br><span class="line">$words  = array(<span class="string">'apple'</span>,<span class="string">'pineapple'</span>,<span class="string">'banana'</span>,<span class="string">'orange'</span>,</span><br><span class="line">                <span class="string">'radish'</span>,<span class="string">'carrot'</span>,<span class="string">'pea'</span>,<span class="string">'bean'</span>,<span class="string">'potato'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 目前没有找到最短距离</span></span><br><span class="line">$shortest = <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 遍历单词来找到最接近的</span></span><br><span class="line">foreach ($words <span class="keyword">as</span> $word) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 计算输入单词与当前单词的距离</span></span><br><span class="line">    $lev = levenshtein($input, $word);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 检查完全的匹配</span></span><br><span class="line">    <span class="keyword">if</span> ($lev == <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 最接近的单词是这个（完全匹配）</span></span><br><span class="line">        $closest = $word;</span><br><span class="line">        $shortest = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 退出循环；我们已经找到一个完全的匹配</span></span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如果此次距离比上次找到的要短</span></span><br><span class="line">    <span class="comment">// 或者还没找到接近的单词</span></span><br><span class="line">    <span class="keyword">if</span> ($lev &lt;= $shortest || $shortest &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 设置最接近的匹配以及它的最短距离https://jc91715.top/code-script/4</span></span><br><span class="line">        $closest  = $word;</span><br><span class="line">        $shortest = $lev;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">echo <span class="string">"Input word: $input\n"</span>;</span><br><span class="line"><span class="keyword">if</span> ($shortest == <span class="number">0</span>) &#123;</span><br><span class="line">    echo <span class="string">"Exact match found: $closest\n"</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    echo <span class="string">"Did you mean: $closest?\n"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="两个文件是否相同"><a href="#两个文件是否相同" class="headerlink" title="两个文件是否相同"></a>两个文件是否相同</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">define(<span class="string">'READ_LEN'</span>, <span class="number">4096</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(files_identical(<span class="string">'file1.txt'</span>, <span class="string">'file2.txt'</span>))</span><br><span class="line">    echo <span class="string">'files identical'</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    echo <span class="string">'files not identical'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//   pass two file names</span></span><br><span class="line"><span class="comment">//   returns TRUE if files are the same, FALSE otherwise</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">files_identical</span>(<span class="params">$fn1, $fn2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(filetype($fn1) !== filetype($fn2))</span><br><span class="line">        <span class="keyword">return</span> FALSE;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(filesize($fn1) !== filesize($fn2))</span><br><span class="line">        <span class="keyword">return</span> FALSE;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!$fp1 = fopen($fn1, <span class="string">'rb'</span>))</span><br><span class="line">        <span class="keyword">return</span> FALSE;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!$fp2 = fopen($fn2, <span class="string">'rb'</span>)) &#123;</span><br><span class="line">        fclose($fp1);</span><br><span class="line">        <span class="keyword">return</span> FALSE;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $same = TRUE;</span><br><span class="line">    <span class="keyword">while</span> (!feof($fp1) and !feof($fp2))</span><br><span class="line">        <span class="keyword">if</span>(fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) &#123;</span><br><span class="line">            $same = FALSE;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(feof($fp1) !== feof($fp2))</span><br><span class="line">        $same = FALSE;</span><br><span class="line"></span><br><span class="line">    fclose($fp1);</span><br><span class="line">    fclose($fp2);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $same;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="大转盘红包概率"><a href="#大转盘红包概率" class="headerlink" title="大转盘红包概率"></a>大转盘红包概率</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPrize</span>(<span class="params">$prizes</span>) </span>&#123;   </span><br><span class="line">    $result=[];</span><br><span class="line">    $weights = array_sum(array_column($prizes,<span class="string">'w'</span>));        </span><br><span class="line">    foreach ($prizes <span class="keyword">as</span> $k =&gt; $prize) &#123;   </span><br><span class="line">        $randNum = mt_rand(<span class="number">1</span>, $weights);   </span><br><span class="line">        <span class="keyword">if</span> ($randNum &lt;= $prize[<span class="string">'w'</span>]) &#123;   </span><br><span class="line">            $result = $prize;   </span><br><span class="line">            <span class="keyword">break</span>;   </span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;   </span><br><span class="line">            $weights -= $prize[<span class="string">'w'</span>];   </span><br><span class="line">        &#125;         </span><br><span class="line">    &#125;     </span><br><span class="line">    <span class="keyword">return</span> $result;   </span><br><span class="line">&#125; </span><br><span class="line">$prizes = array(   </span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">1</span>,<span class="string">'name'</span>=&gt;<span class="string">'特等奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">1</span>),</span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">2</span>,<span class="string">'name'</span>=&gt;<span class="string">'一等奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">5</span>),</span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">3</span>,<span class="string">'name'</span>=&gt;<span class="string">'二等奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">10</span>),</span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">4</span>,<span class="string">'name'</span>=&gt;<span class="string">'三等奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">12</span>),</span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">5</span>,<span class="string">'name'</span>=&gt;<span class="string">'四等奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">22</span>),</span><br><span class="line">    array(<span class="string">'id'</span>=&gt;<span class="number">6</span>,<span class="string">'name'</span>=&gt;<span class="string">'没中奖'</span>,<span class="string">'w'</span>=&gt;<span class="number">50</span>)</span><br><span class="line">); </span><br><span class="line">https:<span class="comment">//jc91715.top/code-script/14</span></span><br><span class="line">var_export(getPrize($prizes));</span><br><span class="line">array (</span><br><span class="line">  <span class="string">'id'</span> =&gt; <span class="number">4</span>,</span><br><span class="line">  <span class="string">'name'</span> =&gt; <span class="string">'三等奖'</span>,</span><br><span class="line">  <span class="string">'w'</span> =&gt; <span class="number">12</span>,</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h3 id="实现http协议"><a href="#实现http协议" class="headerlink" title="实现http协议"></a>实现http协议</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">使用 stream_socket_server 监听tcp连接，并实现http协议。 访问http:<span class="comment">//jc91715.top:8080体验</span></span><br><span class="line">$socket = stream_socket_server(<span class="string">"tcp://0.0.0.0:8080"</span>, $errno, $errstr);</span><br><span class="line"><span class="keyword">if</span> (!$socket) &#123;</span><br><span class="line">          echo <span class="string">"$errstr ($errno)&lt;br /&gt;\n"</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">  echo <span class="string">'正在监听'</span>;https:<span class="comment">//jc91715.top/code-script/16</span></span><br><span class="line">  <span class="keyword">while</span> ($conn = stream_socket_accept($socket)) &#123;</span><br><span class="line">      <span class="comment">//$sock_data = fread($conn, 1024);</span></span><br><span class="line">      <span class="comment">//echo $sock_data;</span></span><br><span class="line">      $res=<span class="string">"HTTP/1.1 200 ok\r\nAccept-Ranges: bytes\r\ncontent-type: text/html; charset=utf-8\r\n\r\nThe local time is "</span>.date(<span class="string">'Y-m-d H:i:s'</span>).<span class="string">"\r\n"</span>;</span><br><span class="line">      fwrite($conn,$res);</span><br><span class="line">          fclose($conn);</span><br><span class="line">      &#125;</span><br><span class="line">        </span><br><span class="line">&#125;</span><br><span class="line">使用 stream_socket_client 创建一个http 客户端。刷新或者访问 http:<span class="comment">//jc91715.top:8080/ 测试</span></span><br><span class="line">$fp = stream_socket_client(<span class="string">"tcp://127.0.0.1:8080"</span>, $errno, $errstr, <span class="number">30</span>);</span><br><span class="line"><span class="keyword">if</span> (!$fp) &#123;</span><br><span class="line">    echo <span class="string">"$errstr ($errno)&lt;br /&gt;\n"</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    fwrite($fp, <span class="string">"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n"</span>);</span><br><span class="line">    <span class="keyword">while</span> (!feof($fp)) &#123;</span><br><span class="line">        echo fgets($fp, <span class="number">1024</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    fclose($fp);</span><br><span class="line">&#125;</span><br><span class="line">HTTP/<span class="number">1.1</span> <span class="number">200</span> ok</span><br><span class="line">Accept-Ranges: bytes</span><br><span class="line">content-type: text/html; charset=utf<span class="number">-8</span></span><br><span class="line"></span><br><span class="line">The local time is <span class="number">2019</span><span class="number">-08</span><span class="number">-01</span> <span class="number">20</span>:<span class="number">24</span>:<span class="number">13</span></span><br></pre></td></tr></table></figure>
<h3 id="排名算法支持重复排名"><a href="#排名算法支持重复排名" class="headerlink" title="排名算法支持重复排名"></a>排名算法支持重复排名</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">$arr = [</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">10</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">30</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">50</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">50</span>,</span><br><span class="line">    ]</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">array_multisort(array_column($arr,<span class="string">'score'</span>),SORT_ASC,$arr);</span><br><span class="line"></span><br><span class="line">$results = array_slice($arr, <span class="number">0</span>, <span class="number">3</span>, <span class="literal">true</span>);</span><br><span class="line">$length = count($arr);</span><br><span class="line">$resultLength = count($results);</span><br><span class="line"></span><br><span class="line"><span class="comment">//$resultLength 比 $length小说明$arr 的还有元素，否则它们俩就相等了。然后比较key和key+1的值。极限是所有的人都和第三名相同，自行判断是否需要所有人都胜出，来限制 $resultLength 的长度https://jc91715.top/code-script/13</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>($resultLength&lt;$length &amp;&amp;$results[$resultLength<span class="number">-1</span>][<span class="string">'score'</span>]==$arr[$resultLength][<span class="string">'score'</span>])&#123;</span><br><span class="line">    array_push($results,$arr[$resultLength]);</span><br><span class="line">    $resultLength = count($results);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$ifWinner = <span class="literal">false</span>;</span><br><span class="line">$id=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(in_array($id,array_column($results,<span class="string">'id'</span>)))&#123;</span><br><span class="line">   $ifWinner = <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var_export($results);</span><br><span class="line">echo <span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">var_export($ifWinner);</span><br></pre></td></tr></table></figure>
<h3 id="new-static-和-new-self"><a href="#new-static-和-new-self" class="headerlink" title="new static 和 new self"></a>new static 和 new self</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Father</span> </span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getSelf</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> self();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getStatic</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="keyword">static</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Son</span> <span class="keyword">extends</span> <span class="title">Father</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">echo get_class(Son::getSelf()); <span class="comment">// Father</span></span><br><span class="line">echo get_class(Son::getStatic()); <span class="comment">// Son</span></span><br><span class="line">echo get_class(Father::getSelf()); <span class="comment">// Father</span></span><br><span class="line">echo get_class(Father::getStatic()); <span class="comment">// Father</span></span><br><span class="line">self 返回的是 <span class="keyword">new</span> self 中关键字 <span class="keyword">new</span> 所在的类中</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> 会返回执行 <span class="keyword">new</span> <span class="keyword">static</span>() 的类，比如 Son 执行 get_class(Son::getStatic()) 返回的是 Son, Father 执行 get_class(Father::getStatic()) 返回的是 Father</span><br><span class="line"></span><br><span class="line">而在没有继承的情况下，可以认为 <span class="keyword">new</span> self 和 <span class="keyword">new</span> <span class="keyword">static</span> 是返回相同的结果。https:<span class="comment">//learnku.com/articles/31929</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-导出-PDF"><a href="#PHP-导出-PDF" class="headerlink" title="PHP 导出 PDF"></a>PHP 导出 PDF</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">require_once(<span class="string">'TCPDF/tcpdf.php'</span>); </span><br><span class="line"><span class="comment">//实例化 https://github.com/tecnickcom/tcpdf</span></span><br><span class="line">$pdf = <span class="keyword">new</span> TCPDF(<span class="string">'P'</span>, <span class="string">'mm'</span>, <span class="string">'A4'</span>, <span class="literal">true</span>, <span class="string">'UTF-8'</span>, <span class="literal">false</span>); </span><br><span class="line"><span class="comment">// 设置文档信息 </span></span><br><span class="line">$pdf-&gt;SetCreator(<span class="string">'Helloweba'</span>); </span><br><span class="line">$pdf-&gt;SetAuthor(<span class="string">'zzy'</span>); </span><br><span class="line">$pdf-&gt;SetTitle(<span class="string">'测试!'</span>); </span><br><span class="line">$pdf-&gt;SetSubject(<span class="string">'TCPDF Tutorial'</span>); </span><br><span class="line">$pdf-&gt;SetKeywords(<span class="string">'TCPDF, PDF, PHP'</span>); </span><br><span class="line"><span class="comment">// 设置页眉和页脚信息 </span></span><br><span class="line">#$pdf-&gt;SetHeaderData('logo.png', 30, '', '',array(0,64,255), array(0,64,128)); </span><br><span class="line">$pdf-&gt;setFooterData(array(<span class="number">0</span>,<span class="number">64</span>,<span class="number">0</span>), array(<span class="number">0</span>,<span class="number">64</span>,<span class="number">128</span>)); </span><br><span class="line"><span class="comment">// 设置页眉和页脚字体 </span></span><br><span class="line">$pdf-&gt;setHeaderFont(<span class="built_in">Array</span>(<span class="string">'stsongstdlight'</span>, <span class="string">''</span>, <span class="string">'10'</span>)); </span><br><span class="line">$pdf-&gt;setFooterFont(<span class="built_in">Array</span>(<span class="string">'helvetica'</span>, <span class="string">''</span>, <span class="string">'8'</span>)); </span><br><span class="line"><span class="comment">// 设置默认等宽字体 </span></span><br><span class="line">$pdf-&gt;SetDefaultMonospacedFont(<span class="string">'courier'</span>); </span><br><span class="line"><span class="comment">// 设置间距 </span></span><br><span class="line">$pdf-&gt;SetMargins(<span class="number">15</span>, <span class="number">27</span>, <span class="number">15</span>); </span><br><span class="line">$pdf-&gt;SetHeaderMargin(<span class="number">5</span>); </span><br><span class="line">$pdf-&gt;SetFooterMargin(<span class="number">10</span>); </span><br><span class="line"><span class="comment">// 设置分页 </span></span><br><span class="line">$pdf-&gt;SetAutoPageBreak(TRUE, <span class="number">25</span>); </span><br><span class="line"><span class="comment">// set image scale factor </span></span><br><span class="line">$pdf-&gt;setImageScale(<span class="number">1.25</span>); </span><br><span class="line"><span class="comment">// set default font subsetting mode </span></span><br><span class="line">$pdf-&gt;setFontSubsetting(<span class="literal">true</span>); </span><br><span class="line"><span class="comment">//设置字体 </span></span><br><span class="line">$pdf-&gt;SetFont(<span class="string">'stsongstdlight'</span>, <span class="string">''</span>, <span class="number">14</span>); </span><br><span class="line">$pdf-&gt;AddPage(); </span><br><span class="line"></span><br><span class="line">$pdf-&gt;Write(<span class="number">0</span>,<span class="string">'折线图'</span>,<span class="number">4</span>, <span class="number">0</span>, <span class="string">'L'</span>, <span class="literal">true</span>, <span class="number">0</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="number">0</span>); </span><br><span class="line">$pdf-&gt;Image(<span class="string">'example12.png'</span>, <span class="number">10</span>, <span class="number">40</span>, <span class="number">200</span>, <span class="number">80</span>, <span class="string">'PNG'</span>, <span class="string">''</span>, <span class="string">''</span>, <span class="literal">true</span>, <span class="number">150</span>, <span class="string">''</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="number">1</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//输出PDF </span></span><br><span class="line">$pdf-&gt;Output(<span class="string">'t.pdf'</span>, <span class="string">'I'</span>);</span><br><span class="line"></span><br><span class="line">redis 本身是单例队列处理的，再多高并发请求都是排队进行。</span><br><span class="line">除了 blpop，还有像 setnx,incrby,decrby,lpop,rpop 等命令，这些你都可以高并发或分布式场景下使用。</span><br></pre></td></tr></table></figure>
<h3 id="strtotime-的困惑"><a href="#strtotime-的困惑" class="headerlink" title="strtotime 的困惑"></a>strtotime 的困惑</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"-1 month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"+1 month"</span>, strtotime(<span class="string">"2017-08-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-10-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"next month"</span>, strtotime(<span class="string">"2017-01-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-03-03</span></span><br><span class="line">只要涉及到大小月的最后一天，都可能会有这个迷惑。</span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last day of -1 month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-02-28</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"first day of +1 month"</span>, strtotime(<span class="string">"2017-08-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-09-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"first day of next month"</span>, strtotime(<span class="string">"2017-01-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-02-01</span></span><br><span class="line">var_dump(date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"last day of last month"</span>, strtotime(<span class="string">"2017-03-31"</span>))));</span><br><span class="line"><span class="comment">//输出2017-02-28</span></span><br></pre></td></tr></table></figure>
<h3 id="正则匹配emoji"><a href="#正则匹配emoji" class="headerlink" title="正则匹配emoji"></a>正则匹配emoji</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/[\w\x&#123;<span class="number">4e00</span>&#125;-\x&#123;<span class="number">9</span>fa5&#125;]&#123;<span class="number">2</span>,<span class="number">25</span>&#125;/u</span><br></pre></td></tr></table></figure>
<h3 id="合并一个或多个数组"><a href="#合并一个或多个数组" class="headerlink" title="合并一个或多个数组"></a>合并一个或多个数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br></pre></td><td class="code"><pre><span class="line">$ar1 = array(<span class="string">"color"</span> =&gt; array(<span class="string">"favorite"</span> =&gt; <span class="string">"red"</span>), <span class="number">5</span>);\</span><br><span class="line">$ar2 = array(<span class="number">10</span>, <span class="string">"color"</span> =&gt; array(<span class="string">"favorite"</span> =&gt; <span class="string">"green"</span>, <span class="string">"blue"</span>));\</span><br><span class="line">$result = array_merge_recursive($ar1, $ar2);</span><br><span class="line"> [</span><br><span class="line">     <span class="string">"color"</span> =&gt; [</span><br><span class="line">       <span class="string">"favorite"</span> =&gt; [</span><br><span class="line">         <span class="string">"red"</span>,</span><br><span class="line">         <span class="string">"green"</span>,</span><br><span class="line">       ],</span><br><span class="line">       <span class="number">0</span> =&gt; <span class="string">"blue"</span>,</span><br><span class="line">     ],</span><br><span class="line">     <span class="number">0</span> =&gt; <span class="number">5</span>,</span><br><span class="line">     <span class="number">1</span> =&gt; <span class="number">10</span>,</span><br><span class="line">   ]</span><br><span class="line">$array1 = array(<span class="string">"a"</span> =&gt; <span class="string">"green"</span>, <span class="string">"b"</span> =&gt; <span class="string">"brown"</span>, <span class="string">"c"</span> =&gt; <span class="string">"blue"</span>, <span class="string">"red"</span>);</span><br><span class="line">$array2 = array(<span class="string">"a"</span> =&gt; <span class="string">"GREEN"</span>, <span class="string">"B"</span> =&gt; <span class="string">"brown"</span>, <span class="string">"yellow"</span>, <span class="string">"red"</span>);</span><br><span class="line">print_r(array_intersect_uassoc($array1, $array2, <span class="string">"strcasecmp"</span>));</span><br><span class="line">带索引检查计算数组的交集，用回调函数比较索引</span><br><span class="line">将一个线性数组转换为一个树，或者多维数组</span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">array_stack</span> (<span class="params">&amp;$a, $p = <span class="string">'@parent'</span>, $c = <span class="string">'@children'</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      $l = $t = array();</span><br><span class="line">      foreach ($a AS $key =&gt; $val):</span><br><span class="line">        <span class="keyword">if</span> (!$val[$p]) $t[$key] =&amp; $l[$key];</span><br><span class="line">        <span class="keyword">else</span> $l[$val[$p]][$c][$key] =&amp; $l[$key];</span><br><span class="line">        $l[$key] = (array)$l[$key] + $val;</span><br><span class="line">      endforeach;</span><br><span class="line">      <span class="keyword">return</span> $a = array(<span class="string">'tree'</span> =&gt; $t, <span class="string">'leaf'</span> =&gt; $l);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $node = array();</span><br><span class="line">    $node[<span class="number">1</span>] = array(<span class="string">'@parent'</span> =&gt; <span class="number">0</span>, <span class="string">'title'</span> =&gt; <span class="string">'I am node 1.'</span>);</span><br><span class="line">    $node[<span class="number">2</span>] = array(<span class="string">'@parent'</span> =&gt; <span class="number">1</span>, <span class="string">'title'</span> =&gt; <span class="string">'I am node 2.'</span>);</span><br><span class="line">    $node[<span class="number">3</span>] = array(<span class="string">'@parent'</span> =&gt; <span class="number">2</span>, <span class="string">'title'</span> =&gt; <span class="string">'I am node 3.'</span>);</span><br><span class="line">    $node[<span class="number">4</span>] = array(<span class="string">'@parent'</span> =&gt; <span class="number">1</span>, <span class="string">'title'</span> =&gt; <span class="string">'I am node 4.'</span>);</span><br><span class="line">    $node[<span class="number">5</span>] = array(<span class="string">'@parent'</span> =&gt; <span class="number">4</span>, <span class="string">'title'</span> =&gt; <span class="string">'I am node 5.'</span>);</span><br><span class="line">    print_r(array_stack($node));</span><br><span class="line"> [</span><br><span class="line">     <span class="string">"tree"</span> =&gt; [</span><br><span class="line">       <span class="number">1</span> =&gt; &amp;<span class="number">1</span> [</span><br><span class="line">         <span class="string">"@parent"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">         <span class="string">"title"</span> =&gt; <span class="string">"I am node 1."</span>,</span><br><span class="line">         <span class="string">"@children"</span> =&gt; [</span><br><span class="line">           <span class="number">2</span> =&gt; &amp;<span class="number">2</span> [</span><br><span class="line">             <span class="string">"@parent"</span> =&gt; <span class="number">1</span>,</span><br><span class="line">             <span class="string">"title"</span> =&gt; <span class="string">"I am node 2."</span>,</span><br><span class="line">             <span class="string">"@children"</span> =&gt; [</span><br><span class="line">               <span class="number">3</span> =&gt; &amp;<span class="number">3</span> [</span><br><span class="line">                 <span class="string">"@parent"</span> =&gt; <span class="number">2</span>,</span><br><span class="line">                 <span class="string">"title"</span> =&gt; <span class="string">"I am node 3."</span>,</span><br><span class="line">               ],</span><br><span class="line">             ],</span><br><span class="line">           ],</span><br><span class="line">           <span class="number">4</span> =&gt; &amp;<span class="number">4</span> [</span><br><span class="line">             <span class="string">"@parent"</span> =&gt; <span class="number">1</span>,</span><br><span class="line">             <span class="string">"title"</span> =&gt; <span class="string">"I am node 4."</span>,</span><br><span class="line">             <span class="string">"@children"</span> =&gt; [</span><br><span class="line">               <span class="number">5</span> =&gt; &amp;<span class="number">5</span> [</span><br><span class="line">                 <span class="string">"@parent"</span> =&gt; <span class="number">4</span>,</span><br><span class="line">                 <span class="string">"title"</span> =&gt; <span class="string">"I am node 5."</span>,</span><br><span class="line">               ],</span><br><span class="line">             ],</span><br><span class="line">           ],</span><br><span class="line">         ],</span><br><span class="line">       ],</span><br><span class="line">     ],</span><br><span class="line">     <span class="string">"leaf"</span> =&gt; [</span><br><span class="line">       <span class="number">1</span> =&gt; &amp;<span class="number">1</span> [],</span><br><span class="line">       <span class="number">2</span> =&gt; &amp;<span class="number">2</span> [],</span><br><span class="line">       <span class="number">3</span> =&gt; &amp;<span class="number">3</span> [],</span><br><span class="line">       <span class="number">4</span> =&gt; &amp;<span class="number">4</span> [],</span><br><span class="line">       <span class="number">5</span> =&gt; &amp;<span class="number">5</span> [],</span><br><span class="line">     ],</span><br><span class="line">   ]</span><br><span class="line">   二维数组分组</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">array_group_by</span>(<span class="params">$arr, $key</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $grouped = [];</span><br><span class="line">       foreach ($arr <span class="keyword">as</span> $value) &#123;</span><br><span class="line">           $grouped[$value[$key]][] = $value;</span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       <span class="keyword">if</span> (func_num_args() &gt; <span class="number">2</span>) &#123;</span><br><span class="line">           $args = func_get_args();</span><br><span class="line">           foreach ($grouped <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">               $parms = array_merge([$value], array_slice($args, <span class="number">2</span>, func_num_args()));</span><br><span class="line">               $grouped[$key] = call_user_func_array(<span class="string">'array_group_by'</span>, $parms);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       <span class="keyword">return</span> $grouped;</span><br><span class="line">   &#125;</span><br><span class="line">   根据日期获取星期</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">get_week</span>(<span class="params">$date</span>)</span>&#123;    <span class="comment">//强制转换日期格式</span></span><br><span class="line">       $date_str=date(<span class="string">'Y-m-d'</span>,strtotime($date));    <span class="comment">//封装成数组</span></span><br><span class="line">       $arr=explode(<span class="string">"-"</span>, $date_str);</span><br><span class="line">       $year=$arr[<span class="number">0</span>];    <span class="comment">//月，输出2位整型，不够2位右对齐</span></span><br><span class="line">       $month=sprintf(<span class="string">'%02d'</span>,$arr[<span class="number">1</span>]);</span><br><span class="line">       $day=sprintf(<span class="string">'%02d'</span>,$arr[<span class="number">2</span>]);    <span class="comment">//时分秒默认赋值为0；</span></span><br><span class="line">       $hour = $minute = $second = <span class="number">0</span>;    <span class="comment">//转换成时间戳</span></span><br><span class="line">       $strap = mktime($hour,$minute,$second,$month,$day,$year);    <span class="comment">//获取数字型星期几</span></span><br><span class="line">       $number_wk=date(<span class="string">"w"</span>,$strap);    <span class="comment">//自定义星期数组</span></span><br><span class="line">       $weekArr=array(<span class="string">"星期日"</span>,<span class="string">"星期一"</span>,<span class="string">"星期二"</span>,<span class="string">"星期三"</span>,<span class="string">"星期四"</span>,<span class="string">"星期五"</span>,<span class="string">"星期六"</span>);    <span class="comment">//获取数字对应的星期</span></span><br><span class="line">       <span class="keyword">return</span> $weekArr[$number_wk];</span><br><span class="line">   &#125;</span><br><span class="line">   计算 两个时间差</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">timediff</span>(<span class="params">$begin_time,$end_time</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">           <span class="keyword">if</span>($begin_time &lt; $end_time)&#123;</span><br><span class="line">               $starttime = $begin_time;</span><br><span class="line">               $endtime = $end_time;</span><br><span class="line">           &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">               $starttime = $end_time;</span><br><span class="line">               $endtime = $begin_time;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="comment">//计算天数</span></span><br><span class="line">           $timediff = $endtime-$starttime;</span><br><span class="line">           $days = intval($timediff/<span class="number">86400</span>);</span><br><span class="line">           <span class="comment">//计算小时数</span></span><br><span class="line">           $remain = $timediff%<span class="number">86400</span>;</span><br><span class="line">           $hours = intval($remain/<span class="number">3600</span>);</span><br><span class="line">           <span class="comment">//计算分钟数</span></span><br><span class="line">           $remain = $remain%<span class="number">3600</span>;</span><br><span class="line">           $mins = intval($remain/<span class="number">60</span>);</span><br><span class="line">           <span class="comment">//计算秒数</span></span><br><span class="line">           $secs = $remain%<span class="number">60</span>;</span><br><span class="line">           $res = array(<span class="string">"day"</span> =&gt; $days,<span class="string">"hour"</span> =&gt; $hours,<span class="string">"min"</span> =&gt; $mins,<span class="string">"sec"</span> =&gt; $secs);</span><br><span class="line">           <span class="keyword">return</span> $res;</span><br><span class="line">   &#125;</span><br><span class="line">   递归遍历目录</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">my_dir</span>(<span class="params">$dir</span>) </span>&#123;</span><br><span class="line">          $files = array();</span><br><span class="line">          $e=mb_detect_encoding($dir,array(<span class="string">'GB2312'</span>,<span class="string">'GBK'</span>,<span class="string">'UTF-8'</span>));</span><br><span class="line">          $dir = mb_convert_encoding($dir,<span class="string">'GBK'</span>, $e);</span><br><span class="line">           <span class="keyword">if</span>(@$handle = opendir($dir)) &#123; <span class="comment">//注意这里要加一个@，不然会有warning错误提示：）</span></span><br><span class="line">               <span class="keyword">while</span>(($file = readdir($handle)) !== <span class="literal">false</span>) &#123;</span><br><span class="line">                   <span class="keyword">if</span>($file != <span class="string">".."</span> &amp;&amp; $file != <span class="string">"."</span>) &#123; <span class="comment">//排除根目录；</span></span><br><span class="line">                       $filename=$dir.<span class="string">"/"</span>.$file;</span><br><span class="line">                       <span class="keyword">if</span>(is_dir($filename)) &#123; <span class="comment">//如果是子文件夹，就进行递归</span></span><br><span class="line">                           $files[$file] =my_dir($filename);</span><br><span class="line">                       &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                           $e=mb_detect_encoding($file,array(<span class="string">'GB2312'</span>,<span class="string">'GBK'</span>,<span class="string">'UTF-8'</span>));</span><br><span class="line">                           $file = mb_convert_encoding($file,<span class="string">'GBK'</span>, $e);</span><br><span class="line">                           $files[] = $file;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">               closedir($handle);</span><br><span class="line">               <span class="keyword">return</span> $files;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   读取文本内容</span><br><span class="line">   </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">auto_read</span>(<span class="params">$file, $charset=<span class="string">'UTF-8'</span></span>) </span>&#123;</span><br><span class="line">           <span class="keyword">if</span> (!file_exists($file)) &#123;</span><br><span class="line">               <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               <span class="keyword">if</span>(filesize($file) &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                       <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">           &#125;</span><br><span class="line">           $list = array(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, <span class="string">'UTF-16LE'</span>, <span class="string">'UTF-16BE'</span>, <span class="string">'ISO-8859-1'</span>);</span><br><span class="line">           $handle = fopen($file,<span class="string">"r"</span>);</span><br><span class="line">           $str = fread($handle,filesize($file));</span><br><span class="line">          foreach ($list <span class="keyword">as</span> $item) &#123;</span><br><span class="line">               $tmp = mb_convert_encoding($str, $item, $item);</span><br><span class="line">               <span class="keyword">if</span> (md5($tmp) == md5($str)) &#123;</span><br><span class="line">                            <span class="keyword">return</span> mb_convert_encoding($str, $charset, $item);</span><br><span class="line">                &#125;</span><br><span class="line">           &#125;</span><br><span class="line">                   <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">       &#125;</span><br><span class="line">   文件大小计算</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">format_size</span>(<span class="params">$size</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $unit = <span class="string">'B'</span>;</span><br><span class="line">       <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">           $size /= <span class="number">1024</span>;</span><br><span class="line">           $unit = <span class="string">'KB'</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">           $size /= <span class="number">1024</span>;</span><br><span class="line">           $unit = <span class="string">'MB'</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">           $size /= <span class="number">1024</span>;</span><br><span class="line">           $unit = <span class="string">'GB'</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">           $size /= <span class="number">1024</span>;</span><br><span class="line">           $unit = <span class="string">'TB'</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> round($size) . $unit;</span><br><span class="line">   &#125;</span><br><span class="line">   中文字符串拆分</span><br><span class="line">   </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">split_cn</span>(<span class="params">$arr</span>)</span>&#123;</span><br><span class="line">       $re=array(); <span class="comment">//定义接受字符串的数组</span></span><br><span class="line">       <span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;mb_strlen($arr);$i++)&#123;</span><br><span class="line">           <span class="keyword">if</span>(mb_substr($arr,$i,<span class="number">1</span>,<span class="string">"utf-8"</span>)==<span class="string">''</span>)&#123;</span><br><span class="line">               <span class="keyword">continue</span>;</span><br><span class="line">           &#125;</span><br><span class="line">           $res[]=mb_substr($arr,$i,<span class="number">1</span>,<span class="string">"utf-8"</span>); <span class="comment">//将单个字符存到数组当中</span></span><br><span class="line">        &#125;</span><br><span class="line">       <span class="keyword">return</span> $res;</span><br><span class="line">   &#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/31987</span></span><br></pre></td></tr></table></figure>
<h3 id="存储货币"><a href="#存储货币" class="headerlink" title="存储货币"></a>存储货币</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">setPriceAttribute</span>(<span class="params">$value</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $<span class="keyword">this</span>-&gt;attributes[<span class="string">'price] = intval($value * 100);//以分为单位https://learnku.com/articles/31968</span></span><br><span class="line"><span class="string">   &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">   public function getPriceAttribute($value)</span></span><br><span class="line"><span class="string">   &#123;</span></span><br><span class="line"><span class="string">       return $value / 100;</span></span><br><span class="line"><span class="string">   &#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="MySQL-Blob-类型"><a href="#MySQL-Blob-类型" class="headerlink" title="MySQL Blob 类型"></a>MySQL Blob 类型</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$filename = <span class="string">'filename.txt'</span>;</span><br><span class="line">$headers = [</span><br><span class="line">    <span class="string">'Content-Encoding'</span> =&gt; <span class="string">'UTF-8'</span>,</span><br><span class="line">    <span class="string">'Content-Type'</span> =&gt; <span class="string">'text/html;charset=UTF-8'</span>,<span class="comment">//application/x-msdownload </span></span><br><span class="line">    <span class="string">'Content-Disposition'</span> =&gt; <span class="string">"attachment; filename=\"$filename\""</span>,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">response()-&gt;stream(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $handle = fopen(<span class="string">'php://output'</span>, <span class="string">'w'</span>);</span><br><span class="line">    fwrite($handle, <span class="string">'something contents'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Close the output stream</span></span><br><span class="line">    fclose($handle);</span><br><span class="line">&#125;, <span class="number">200</span>, $headers)-&gt;send();</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/32066</span></span><br></pre></td></tr></table></figure>
<h3 id="中文分割"><a href="#中文分割" class="headerlink" title="中文分割"></a>中文分割</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">split_cn</span>(<span class="params">$arr</span>)</span>&#123;</span><br><span class="line">    $re=array(); <span class="comment">//定义接受字符串的数组</span></span><br><span class="line">    <span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;mb_strlen($arr);$i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(mb_substr($arr,$i,<span class="number">1</span>,<span class="string">"utf-8"</span>)==<span class="string">''</span>)&#123;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $res[]=mb_substr($arr,$i,<span class="number">1</span>,<span class="string">"utf-8"</span>); <span class="comment">//将单个字符存到数组当中</span></span><br><span class="line">     &#125;</span><br><span class="line">    <span class="keyword">return</span> $res;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"> preg_split(<span class="string">'//u'</span>,<span class="string">'中文'</span>, <span class="literal">null</span>, PREG_SPLIT_NO_EMPTY)</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="string">"中"</span>,</span><br><span class="line">     <span class="string">"文"</span>,</span><br><span class="line">   ]</span><br><span class="line">    split_cn(<span class="string">'中文'</span>)    </span><br><span class="line">   [                  </span><br><span class="line">     <span class="string">"中"</span>,             </span><br><span class="line">     <span class="string">"文"</span>,             </span><br><span class="line">   ]                  </span><br><span class="line">   preg_split(<span class="string">'/(?&lt;!^)(?!$)/u'</span>, $string );  https:<span class="comment">//www.php.net/manual/zh/function.mb-split.php</span></span><br></pre></td></tr></table></figure>
<h3 id="文件大小"><a href="#文件大小" class="headerlink" title="文件大小"></a>文件大小</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">format_size</span>(<span class="params">$size</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $unit = <span class="string">'B'</span>;</span><br><span class="line">    <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">        $size /= <span class="number">1024</span>;</span><br><span class="line">        $unit = <span class="string">'KB'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">        $size /= <span class="number">1024</span>;</span><br><span class="line">        $unit = <span class="string">'MB'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">        $size /= <span class="number">1024</span>;</span><br><span class="line">        $unit = <span class="string">'GB'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($size &gt;= <span class="number">1024</span>) &#123;</span><br><span class="line">        $size /= <span class="number">1024</span>;</span><br><span class="line">        $unit = <span class="string">'TB'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> round($size) . $unit;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 格式化字节大小</span></span><br><span class="line"><span class="comment">* @param  number $size      字节数</span></span><br><span class="line"><span class="comment">* @param  string $delimiter 数字和单位分隔符</span></span><br><span class="line"><span class="comment">* @return string            格式化后的带单位的大小</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_byte</span>(<span class="params">$size, $delimiter = <span class="string">''</span></span>) </span>&#123;</span><br><span class="line">$units = array(<span class="string">'B'</span>, <span class="string">'KB'</span>, <span class="string">'MB'</span>, <span class="string">'GB'</span>, <span class="string">'TB'</span>, <span class="string">'PB'</span>);</span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $size &gt;= <span class="number">1024</span> &amp;&amp; $i &lt; <span class="number">5</span>; $i++) $size /= <span class="number">1024</span>;</span><br><span class="line"><span class="keyword">return</span> round($size, <span class="number">2</span>) . $delimiter . $units[$i];</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/31985</span></span><br></pre></td></tr></table></figure>
<h3 id="时间差"><a href="#时间差" class="headerlink" title="时间差"></a>时间差</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">timediff</span>(<span class="params">$begin_time,$end_time</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($begin_time &lt; $end_time)&#123;</span><br><span class="line">            $starttime = $begin_time;</span><br><span class="line">            $endtime = $end_time;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            $starttime = $end_time;</span><br><span class="line">            $endtime = $begin_time;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//计算天数</span></span><br><span class="line">        $timediff = $endtime-$starttime;</span><br><span class="line">        $days = intval($timediff/<span class="number">86400</span>);</span><br><span class="line">        <span class="comment">//计算小时数</span></span><br><span class="line">        $remain = $timediff%<span class="number">86400</span>;</span><br><span class="line">        $hours = intval($remain/<span class="number">3600</span>);</span><br><span class="line">        <span class="comment">//计算分钟数</span></span><br><span class="line">        $remain = $remain%<span class="number">3600</span>;</span><br><span class="line">        $mins = intval($remain/<span class="number">60</span>);</span><br><span class="line">        <span class="comment">//计算秒数</span></span><br><span class="line">        $secs = $remain%<span class="number">60</span>;</span><br><span class="line">        $res = array(<span class="string">"day"</span> =&gt; $days,<span class="string">"hour"</span> =&gt; $hours,<span class="string">"min"</span> =&gt; $mins,<span class="string">"sec"</span> =&gt; $secs);</span><br><span class="line">        <span class="keyword">return</span> $res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="数组分组"><a href="#数组分组" class="headerlink" title="数组分组"></a>数组分组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">array_group_by</span>(<span class="params">$arr, $key</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $grouped = [];</span><br><span class="line">    foreach ($arr <span class="keyword">as</span> $value) &#123;</span><br><span class="line">        $grouped[$value[$key]][] = $value;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (func_num_args() &gt; <span class="number">2</span>) &#123;</span><br><span class="line">        $args = func_get_args();</span><br><span class="line">        foreach ($grouped <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            $parms = array_merge([$value], array_slice($args, <span class="number">2</span>, func_num_args()));</span><br><span class="line">            $grouped[$key] = call_user_func_array(<span class="string">'array_group_by'</span>, $parms);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $grouped;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="日期获取星期"><a href="#日期获取星期" class="headerlink" title="日期获取星期"></a>日期获取星期</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_week</span>(<span class="params">$date</span>)</span>&#123;    <span class="comment">//强制转换日期格式</span></span><br><span class="line">    $date_str=date(<span class="string">'Y-m-d'</span>,strtotime($date));    <span class="comment">//封装成数组</span></span><br><span class="line">    $arr=explode(<span class="string">"-"</span>, $date_str);</span><br><span class="line">    $year=$arr[<span class="number">0</span>];    <span class="comment">//月，输出2位整型，不够2位右对齐</span></span><br><span class="line">    $month=sprintf(<span class="string">'%02d'</span>,$arr[<span class="number">1</span>]);</span><br><span class="line">    $day=sprintf(<span class="string">'%02d'</span>,$arr[<span class="number">2</span>]);    <span class="comment">//时分秒默认赋值为0；</span></span><br><span class="line">    $hour = $minute = $second = <span class="number">0</span>;    <span class="comment">//转换成时间戳</span></span><br><span class="line">    $strap = mktime($hour,$minute,$second,$month,$day,$year);    <span class="comment">//获取数字型星期几</span></span><br><span class="line">    $number_wk=date(<span class="string">"w"</span>,$strap);    <span class="comment">//自定义星期数组</span></span><br><span class="line">    $weekArr=array(<span class="string">"星期日"</span>,<span class="string">"星期一"</span>,<span class="string">"星期二"</span>,<span class="string">"星期三"</span>,<span class="string">"星期四"</span>,<span class="string">"星期五"</span>,<span class="string">"星期六"</span>);    <span class="comment">//获取数字对应的星期</span></span><br><span class="line">    <span class="keyword">return</span> $weekArr[$number_wk];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-距离排序"><a href="#Laravel-距离排序" class="headerlink" title="Laravel 距离排序"></a>Laravel 距离排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getDistance</span>(<span class="params">$lat, $lng</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $distance = <span class="string">"ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(( $lat* 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(( $lng* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380"</span>;</span><br><span class="line">    $list     = $<span class="keyword">this</span>-&gt;model-&gt;select(<span class="string">'*'</span>)-&gt;addSelect(\DB::raw($distance . <span class="string">' as distance'</span>))-&gt;orderBy(<span class="string">'distance'</span>, <span class="string">'ASC'</span>)-&gt;get()-&gt;toArray();</span><br><span class="line">    dd($list);</span><br><span class="line">&#125;</span><br><span class="line">distance 是计算出来的距离单位是公里</span><br><span class="line">$model = DB::table(<span class="string">'table_name'</span>)-&gt;select(<span class="string">''</span>);</span><br><span class="line">$model-&gt;addSelect(\DB::raw(<span class="string">"acos(cos("</span> . $lat . <span class="string">"*pi()/180)*cos(lat*pi()/180)*cos("</span> . $lng . <span class="string">"*pi()/180-lng*pi()/180)+sin("</span> . $lat . <span class="string">"*pi()/180)*sin(lat * pi()/180)) * 6367000 AS distance"</span>))-&gt;orderBy(<span class="string">'distance'</span>,<span class="string">'ASC'</span>)-&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32166</span></span><br></pre></td></tr></table></figure>
<h3 id="二维数组去重"><a href="#二维数组去重" class="headerlink" title="二维数组去重"></a>二维数组去重</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">assoc_unique</span>(<span class="params">&amp;$arr, $key</span>)</span>&#123;</span><br><span class="line">    $rAr = array();</span><br><span class="line">    <span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;count($arr);$i++) &#123;</span><br><span class="line">        <span class="keyword">if</span>(!isset($rAr[$arr[$i][$key]])) &#123;</span><br><span class="line">            $rAr[$arr[$i][$key]]=$arr[$i];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array_values($rAr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">list_sort_by</span>(<span class="params">$list,$field, $sortby=<span class="string">'asc'</span></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">if</span>(is_array($list))&#123;</span><br><span class="line">       $refer = $resultSet = array();</span><br><span class="line">       foreach ($list <span class="keyword">as</span> $i =&gt; $data)</span><br><span class="line">           $refer[$i] = &amp;$data[$field];</span><br><span class="line">       <span class="keyword">switch</span> ($sortby) &#123;</span><br><span class="line">           <span class="keyword">case</span> <span class="string">'asc'</span>: <span class="comment">// 正向排序</span></span><br><span class="line">                asort($refer);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">           <span class="keyword">case</span> <span class="string">'desc'</span>:<span class="comment">// 逆向排序</span></span><br><span class="line">                arsort($refer);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">           <span class="keyword">case</span> <span class="string">'nat'</span>: <span class="comment">// 自然排序https://learnku.com/articles/32011#1c030d</span></span><br><span class="line">                natcasesort($refer);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       foreach ( $refer <span class="keyword">as</span> $key=&gt; $val)</span><br><span class="line">           $resultSet[] = &amp;$list[$key];</span><br><span class="line">       <span class="keyword">return</span> $resultSet;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字符串转换成数组"><a href="#字符串转换成数组" class="headerlink" title="字符串转换成数组"></a>字符串转换成数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">strToArray</span>(<span class="params">$string</span>) </span>&#123;</span><br><span class="line">    $strlen = mb_strlen($string);</span><br><span class="line">    <span class="keyword">while</span> ($strlen) &#123;</span><br><span class="line">        $array[] = mb_substr($string, <span class="number">0</span>, <span class="number">1</span>, <span class="string">"utf8"</span>);</span><br><span class="line">        $string = mb_substr($string, <span class="number">1</span>, $strlen, <span class="string">"utf8"</span>);</span><br><span class="line">        $strlen = mb_strlen($string);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $array;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="创建图片上传目录"><a href="#创建图片上传目录" class="headerlink" title="创建图片上传目录"></a>创建图片上传目录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">my_mkdir</span>(<span class="params">$Folder</span>)</span>&#123; <span class="comment">// 创建图片上传目录和缩略图目录</span></span><br><span class="line">    <span class="keyword">if</span>(!is_dir($Folder))&#123;</span><br><span class="line">        $dir = explode(<span class="string">'/'</span>,$Folder);</span><br><span class="line">        foreach($dir <span class="keyword">as</span> $v)&#123;</span><br><span class="line">            <span class="keyword">if</span>($v)&#123;</span><br><span class="line">                $d .= $v . <span class="string">'/'</span>;</span><br><span class="line">                <span class="keyword">if</span>(!is_dir($d))&#123;</span><br><span class="line">                    $state = mkdir($d);</span><br><span class="line">                    <span class="keyword">if</span>(!$state)&#123;</span><br><span class="line">                        die(<span class="string">'在创建目录'</span> . $d . <span class="string">'时出错！'</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="删除目录"><a href="#删除目录" class="headerlink" title="删除目录"></a>删除目录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除目录及目录下所有文件或删除指定文件</span></span><br><span class="line"><span class="comment"> * @param str $path   待删除目录路径</span></span><br><span class="line"><span class="comment"> * @param int $delDir 是否删除目录，1或true删除目录，0或false则只删除文件保留目录（包含子目录）</span></span><br><span class="line"><span class="comment"> * @return bool 返回删除状态</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">del_dir_and_file</span>(<span class="params">$path, $delDir = FALSE</span>) </span>&#123;</span><br><span class="line">    $handle = opendir($path);</span><br><span class="line">    <span class="keyword">if</span> ($handle) &#123;</span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">false</span> !== ( $item = readdir($handle) )) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($item != <span class="string">"."</span> &amp;&amp; $item != <span class="string">".."</span>)</span><br><span class="line">                is_dir(<span class="string">"$path/$item"</span>) ? del_dir_and_file(<span class="string">"$path/$item"</span>, $delDir) : unlink(<span class="string">"$path/$item"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        closedir($handle);</span><br><span class="line">        <span class="keyword">if</span> ($delDir)</span><br><span class="line">            <span class="keyword">return</span> rmdir($path);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (file_exists($path)) &#123;</span><br><span class="line">            <span class="keyword">return</span> unlink($path);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> FALSE;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="xml-转为-array"><a href="#xml-转为-array" class="headerlink" title="xml 转为 array"></a>xml 转为 array</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">xmlToArray</span>(<span class="params">$xml</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//将XML转为array</span></span><br><span class="line">    $array_data = json_decode(json_encode(simplexml_load_string($xml, <span class="string">'SimpleXMLElement'</span>, LIBXML_NOCDATA)), <span class="literal">true</span>);</span><br><span class="line">    <span class="keyword">return</span> $array_data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="递归重组"><a href="#递归重组" class="headerlink" title="递归重组"></a>递归重组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">node_merge</span>(<span class="params">$attr, $arr</span>) </span>&#123;</span><br><span class="line">    foreach($attr <span class="keyword">as</span> $v)&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_array($arr))&#123;</span><br><span class="line">            $v[<span class="string">'access'</span>] = in_array($v[<span class="string">'id'</span>],$arr) ? <span class="number">1</span>: <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $attr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取文件信息"><a href="#获取文件信息" class="headerlink" title="获取文件信息"></a>获取文件信息</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_file_info</span>(<span class="params">$filepath=<span class="string">''</span>, $key=<span class="string">''</span></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//打开文件，r表示以只读方式打开</span></span><br><span class="line">    $handle = fopen($filepath,<span class="string">"r"</span>);</span><br><span class="line">    <span class="comment">//获取文件的统计信息</span></span><br><span class="line">    $fstat = fstat($handle);</span><br><span class="line"></span><br><span class="line">    fclose($handle);</span><br><span class="line">    $fstat[<span class="string">'filename'</span>] = basename($filepath);</span><br><span class="line">    <span class="keyword">if</span>(!empty($key))&#123;</span><br><span class="line">        <span class="keyword">return</span> $fstat[$key];</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $fstat;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="非递归实现查询该目录下所有文件"><a href="#非递归实现查询该目录下所有文件" class="headerlink" title="非递归实现查询该目录下所有文件"></a>非递归实现查询该目录下所有文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * PHP 非递归实现查询该目录下所有文件</span></span><br><span class="line"><span class="comment"> * @param unknown $dir</span></span><br><span class="line"><span class="comment"> * @return multitype:|multitype:string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">scanfiles</span>(<span class="params">$dir</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (! is_dir ( $dir )) <span class="keyword">return</span> array ();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 兼容各操作系统</span></span><br><span class="line">    $dir = rtrim ( str_replace ( <span class="string">'\\'</span>, <span class="string">'/'</span>, $dir ), <span class="string">'/'</span> ) . <span class="string">'/'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 栈，默认值为传入的目录</span></span><br><span class="line">    $dirs = array ( $dir );</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 放置所有文件的容器</span></span><br><span class="line">    $rt = array ();</span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        <span class="comment">// 弹栈</span></span><br><span class="line">        $dir = array_pop ( $dirs );</span><br><span class="line">        <span class="comment">// 扫描该目录</span></span><br><span class="line">        $tmp = scandir ( $dir );</span><br><span class="line">        foreach ( $tmp <span class="keyword">as</span> $f ) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 过滤. ..</span></span><br><span class="line">            <span class="keyword">if</span> ($f == <span class="string">'.'</span> || $f == <span class="string">'..'</span>)</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 组合当前绝对路径</span></span><br><span class="line">            $path = $dir . $f;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 如果是目录，压栈。</span></span><br><span class="line">            <span class="keyword">if</span> (is_dir ( $path )) &#123;</span><br><span class="line">                array_push ( $dirs, $path . <span class="string">'/'</span> );</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (is_file ( $path )) &#123; <span class="comment">// 如果是文件，放入容器中</span></span><br><span class="line">                $rt [] = $path;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">while</span> ( $dirs ); <span class="comment">// 直到栈中没有目录</span></span><br><span class="line">    <span class="keyword">return</span> $rt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="单例cron"><a href="#单例cron" class="headerlink" title="单例cron"></a>单例cron</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">同时消费队列的进程不得超过<span class="number">1</span>个</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $pid     = getmypid();</span><br><span class="line">        $pidFile = self::PID_FILE;</span><br><span class="line">        <span class="keyword">if</span> (file_exists($pidFile)) &#123;</span><br><span class="line">            $pid    = file_get_contents($pidFile);</span><br><span class="line">            $output = shell_exec(<span class="string">"ps aux | grep -v grep | grep &#123;$pid&#125; | grep php | grep index_prod | grep balabala"</span>);</span><br><span class="line">            <span class="keyword">if</span> (!empty($output)) &#123;</span><br><span class="line">                exit(<span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        file_put_contents($pidFile, $pid);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">100000</span>; $i++) &#123;</span><br><span class="line">            <span class="comment">// bla bla</span></span><br><span class="line">        &#125;</span><br><span class="line">        unlink($pidFile);</span><br><span class="line">    &#125;</span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;redis-&gt;exists(self::PID_KEY)) &#123;</span><br><span class="line">                retry_loop:$pid    = $<span class="keyword">this</span>-&gt;redis-&gt;get(self::PID_KEY);</span><br><span class="line">                $output = shell_exec(<span class="string">"ps aux | grep -v grep | grep &#123;$pid&#125; | grep php | grep index_prod | grep AsyncBatchMemberImport"</span>);</span><br><span class="line">                <span class="keyword">if</span> (!empty($output)) &#123;</span><br><span class="line">                    exit(<span class="number">0</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            $ret = $<span class="keyword">this</span>-&gt;redis-&gt;set(self::PID_KEY, getmypid(), array(<span class="string">'nx'</span>));</span><br><span class="line">            <span class="keyword">if</span> (<span class="literal">false</span> === $ret) &#123;</span><br><span class="line">                goto retry_loop;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">100000</span>; $i++) &#123;</span><br><span class="line">                <span class="comment">// bla bla</span></span><br><span class="line">            &#125;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">        &#125;</span><br><span class="line">        php.ini中disable_functions包含了这个函数 getmypid返回的结果是<span class="literal">null</span></span><br><span class="line">        php script.php <span class="string">`cat /dev/urandom | head -n 10 | md5sum | head -c 10`</span></span><br><span class="line">cat /dev/urandom | head -n <span class="number">10</span> | md5sum | head -c <span class="number">10</span> 返回随机数 https:<span class="comment">//blog.jiaojie.site/_posts/2017/12/13/another-cron-pid/</span></span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">$uniqueId = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;uniqueId = $uniqueId;</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;redis-&gt;exists(self::PID_KEY)) &#123;</span><br><span class="line">            $pid = $<span class="keyword">this</span>-&gt;redis-&gt;get(self::PID_KEY);</span><br><span class="line">            log_warning(<span class="string">'ERP_IMPORT_ASYNC_INFO'</span>, <span class="string">"UniqueId &#123;$pid&#125; Still RUNNING"</span>);</span><br><span class="line">            exit(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $ret = $<span class="keyword">this</span>-&gt;redis-&gt;set(self::PID_KEY, $uniqueId, array(<span class="string">'nx'</span>));</span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">false</span> === $ret) &#123;</span><br><span class="line">            log_warning(<span class="string">'ERP_IMPORT_ASYNC_INFO'</span>, <span class="string">"Set PID FAILED, WILL RETRY, Current PID "</span> . $uniqueId);</span><br><span class="line">            exit(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;redis-&gt;expire(self::PID_KEY, <span class="number">360</span>);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">1000</span>; $i++) &#123;</span><br><span class="line">            <span class="comment">// bla bla</span></span><br><span class="line">        &#125;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;redis-&gt;del(self::PID_KEY);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="每三位加个逗号分割"><a href="#每三位加个逗号分割" class="headerlink" title="每三位加个逗号分割"></a>每三位加个逗号分割</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$input = <span class="string">"12345678"</span>;</span><br><span class="line"></span><br><span class="line">$regex = <span class="string">"#^(?&lt;first&gt;([0-9]&#123;1,3&#125;)??)(?&lt;others&gt;([0-9]&#123;3&#125;)*)$#"</span>;</span><br><span class="line"></span><br><span class="line">$callback = <span class="function"><span class="keyword">function</span>(<span class="params">$match</span>) </span>&#123;</span><br><span class="line">    $arr = str_split($match[<span class="string">"others"</span>], <span class="number">3</span>);</span><br><span class="line">    <span class="keyword">return</span> empty($match[<span class="string">"first"</span>]) ? (implode(<span class="string">","</span>, $arr)) : (<span class="string">""</span> . $match[<span class="string">"first"</span>] . <span class="string">","</span> . implode(<span class="string">","</span>, $arr));</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$output = preg_replace_callback($regex, $callback, $input);<span class="comment">//12,345,678</span></span><br></pre></td></tr></table></figure>
<h3 id="Fibonacci数列"><a href="#Fibonacci数列" class="headerlink" title="Fibonacci数列"></a>Fibonacci数列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">$fib = <span class="function"><span class="keyword">function</span>(<span class="params">$n</span>)<span class="title">use</span>(<span class="params">&amp;$fib</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>($n == <span class="number">0</span> || $n == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> $fib($n - <span class="number">1</span>) + $fib($n - <span class="number">2</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">echo $fib(<span class="number">10</span>);</span><br><span class="line">php的匿名函数中，如果use一个基本数据类型（非对象）的时候，传递的是当时此数据的快照；而use一个对象的时候，与函数参数中传递该对象相同，都是引用</span><br><span class="line">https:<span class="comment">//blog.jiaojie.site/_posts/2017/07/25/the-word-use-in-php-closure/</span></span><br><span class="line">$aaa = <span class="number">111</span>;</span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span>(<span class="params">$aaa</span>)</span>&#123; print $aaa; &#125;;</span><br><span class="line">$aaa = <span class="number">222</span>;</span><br><span class="line">$func(); <span class="comment">// Outputs "111"</span></span><br><span class="line"></span><br><span class="line">$aaa = <span class="number">111</span>;</span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span>(<span class="params">&amp;$aaa</span>)</span>&#123; print $aaa; &#125;;</span><br><span class="line">$aaa = <span class="number">222</span>;</span><br><span class="line">$func(); <span class="comment">// Outputs "222"</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public $foo = <span class="string">'default'</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$foo = <span class="keyword">new</span> Foo;</span><br><span class="line"></span><br><span class="line">$func = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span> (<span class="params">$foo</span>) </span>&#123;</span><br><span class="line">    echo $foo-&gt;foo . <span class="string">"\n"</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$foo-&gt;foo = <span class="string">'changable'</span>;</span><br><span class="line">$func();<span class="comment">// 输出 "changable"</span></span><br></pre></td></tr></table></figure>
<h3 id="验证日期非HTML字符"><a href="#验证日期非HTML字符" class="headerlink" title="验证日期非HTML字符"></a>验证日期非HTML字符</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">date(<span class="string">'Y-m-d'</span>,strtotime(<span class="string">'2019-09-09'</span>))==<span class="string">'2019-09-09'</span></span><br><span class="line"><span class="keyword">if</span> ($content == strip_tags($content))&#123;<span class="comment">//没有HTML标签</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="ip限制"><a href="#ip限制" class="headerlink" title="ip限制"></a>ip限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ipLimit</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  $meip = ip2long($_SERVER[<span class="string">'REMOTE_ADDR'</span>]);</span><br><span class="line">  $ip_lib= array(<span class="string">'10.0.0.0-10.255.255.255'</span>);</span><br><span class="line">  <span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;count($ip_lib);$i++)&#123;</span><br><span class="line">    list($sip,$eip) = explode(<span class="string">'-'</span>,$ip_lib[$i]);</span><br><span class="line">    $sip = ip2long(trim($sip));</span><br><span class="line">    $eip = ip2long(trim($eip));</span><br><span class="line">    <span class="keyword">if</span>($meip &lt;= $sip || $meip &gt;= $eip)&#123;</span><br><span class="line">      header(<span class="string">"Location:http://sina.com.cn/"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP-导出-Excel"><a href="#PHP-导出-Excel" class="headerlink" title="PHP 导出 Excel"></a>PHP 导出 Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**\</span></span><br><span class="line"><span class="comment">* 导出Excel文件 速度慢</span></span><br><span class="line"><span class="comment">* @param $fileName 导出的文件名</span></span><br><span class="line"><span class="comment">* @param $headArr 数据头</span></span><br><span class="line"><span class="comment">* @param $data 导出数据</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getExcel</span>(<span class="params">$fileName,$headArr,$data</span>)</span>&#123;</span><br><span class="line">  <span class="comment">//设置PHP最大单线程的独立内存使用量</span></span><br><span class="line">  ini_set(<span class="string">'memory_limit'</span>,<span class="string">'1024M'</span>);</span><br><span class="line">  <span class="comment">//程序超时设置设为不限时</span></span><br><span class="line">  ini_set(<span class="string">'max_execution_time '</span>,<span class="string">'0'</span>);</span><br><span class="line"></span><br><span class="line">  <span class="comment">//导入PHPExcel类库，因为PHPExcel没有用命名空间，所以使用vendor导入</span></span><br><span class="line">  vendor(<span class="string">"PHPExcel.PHPExcel.IOFactory"</span>);</span><br><span class="line">  vendor(<span class="string">"Excel.PHPExcel"</span>);</span><br><span class="line">  vendor(<span class="string">"Excel.PHPExcel.Writer.Excel5"</span>);</span><br><span class="line">  vendor(<span class="string">"Excel.PHPExcel.IOFactory.php"</span>);</span><br><span class="line"></span><br><span class="line">  <span class="comment">//对数据进行检验</span></span><br><span class="line">  <span class="keyword">if</span>(empty($data) || !is_array($data))&#123;</span><br><span class="line">      die(<span class="string">"data must be a array"</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//检查文件名</span></span><br><span class="line">  <span class="keyword">if</span>(empty($fileName))&#123;</span><br><span class="line">      exit;</span><br><span class="line">  &#125;</span><br><span class="line">  $date = date(<span class="string">"Y_m_d"</span>,time());</span><br><span class="line">  $fileName .= <span class="string">"_&#123;$date&#125;.xls"</span>;</span><br><span class="line">  <span class="comment">//创建PHPExcel对象</span></span><br><span class="line">  $objPHPExcel = <span class="keyword">new</span> \PHPExcel();</span><br><span class="line"></span><br><span class="line">  <span class="comment">//设置表头</span></span><br><span class="line">  $key = ord(<span class="string">"A"</span>);</span><br><span class="line">  foreach($headArr <span class="keyword">as</span> $hkey =&gt; $v)&#123;</span><br><span class="line">      $colum = chr($key);</span><br><span class="line">      $objPHPExcel-&gt;setActiveSheetIndex(<span class="number">0</span>) -&gt;setCellValue($colum.<span class="string">'1'</span>, $v);</span><br><span class="line">      $key += <span class="number">1</span>;</span><br><span class="line">      unset($headArr[$hkey]);</span><br><span class="line">  &#125;</span><br><span class="line">  $column = <span class="number">2</span>;</span><br><span class="line">  $objActSheet = $objPHPExcel-&gt;getActiveSheet();</span><br><span class="line">  foreach($data <span class="keyword">as</span> $key =&gt; $rows)&#123; <span class="comment">//行写入</span></span><br><span class="line">      $span = ord(<span class="string">"A"</span>);</span><br><span class="line">      foreach($rows <span class="keyword">as</span> $keyName=&gt;$value)&#123;<span class="comment">// 列写入</span></span><br><span class="line">          $j = chr($span);</span><br><span class="line">          <span class="comment">//设置导出单元格格式为文本，避免身份证号的数据被Excel改写</span></span><br><span class="line">          $objActSheet-&gt;setCellValueExplicit($j.$column, $value);</span><br><span class="line">          $span++;</span><br><span class="line">          unset($rows[$keyName]);</span><br><span class="line">      &#125;</span><br><span class="line">      $column++;</span><br><span class="line">      unset($data[$key]);</span><br><span class="line">  &#125;</span><br><span class="line">  $fileName = iconv(<span class="string">"utf-8"</span>, <span class="string">"gb2312"</span>, $fileName);</span><br><span class="line">  <span class="comment">//重命名表</span></span><br><span class="line">  <span class="comment">// $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('test');</span></span><br><span class="line">  <span class="comment">//设置活动单指数到第一个表,所以Excel打开这是第一个表</span></span><br><span class="line">  $objPHPExcel-&gt;setActiveSheetIndex(<span class="number">0</span>);</span><br><span class="line">  ob_end_clean();</span><br><span class="line">  ob_start();</span><br><span class="line">  header(<span class="string">'Content-Type: application/vnd.ms-excel'</span>);<span class="comment">//定义输出的文件类型为excel文件</span></span><br><span class="line">  header(<span class="string">"Content-Disposition: attachment;filename=\"$fileName\""</span>);<span class="comment">//定义输出的文件名</span></span><br><span class="line">  header(<span class="string">'Cache-Control: max-age=0'</span>);<span class="comment">//强制每次请求直接发送给源服务器，而不经过本地缓存版本的校验。</span></span><br><span class="line">  $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, <span class="string">'Excel2007'</span>);</span><br><span class="line">  $objWriter-&gt;save(<span class="string">'php://output'</span>); <span class="comment">//文件通过浏览器下载</span></span><br><span class="line">  exit;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">表数据限制：</span><br><span class="line"></span><br><span class="line">Excel <span class="number">2003</span> 及以下的版本。一张表最大支持 <span class="number">65536</span> 行数据，<span class="number">256</span> 列。</span><br><span class="line"></span><br><span class="line">Excel <span class="number">2007</span><span class="number">-2010</span> 版本。一张表最大支持 <span class="number">1048576</span> 行，<span class="number">16384</span> 列。</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 导出CSV数据处理</span></span><br><span class="line"><span class="comment">     * 待优化项：如果导出数据达到百万以上，需要做分批导出CSV文件再添加到压缩文件统一打包下载</span></span><br><span class="line"><span class="comment">     * @desc 数据导出到csv(csv文件)</span></span><br><span class="line"><span class="comment">     * @param string $filename 文件名称</span></span><br><span class="line"><span class="comment">     * @param array $tileArray 所有列名称</span></span><br><span class="line"><span class="comment">     * @param array $dataArray 所有列数据</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">exportToCsv</span>(<span class="params">$filename, $tileArray=array(</span>), <span class="title">$dataArray</span>=<span class="title">array</span>(<span class="params"></span>))</span>&#123;</span><br><span class="line">        <span class="comment">//设置PHP最大单线程的独立内存使用量</span></span><br><span class="line">        ini_set(<span class="string">'memory_limit'</span>,<span class="string">'1024M'</span>);</span><br><span class="line">        <span class="comment">//程序超时设置设为不限时</span></span><br><span class="line">        ini_set(<span class="string">'max_execution_time '</span>,<span class="string">'0'</span>);</span><br><span class="line">        ob_end_clean();</span><br><span class="line">        ob_start();</span><br><span class="line">        header(<span class="string">"Content-Type: text/csv"</span>);</span><br><span class="line">        $filename .= date(<span class="string">"Y-m-d"</span>).<span class="string">".csv"</span>;</span><br><span class="line">        header(<span class="string">"Content-Disposition:filename="</span>.$filename);</span><br><span class="line">        $fp=fopen(<span class="string">'php://output'</span>,<span class="string">'w'</span>);</span><br><span class="line">        fwrite($fp, chr(<span class="number">0xEF</span>).chr(<span class="number">0xBB</span>).chr(<span class="number">0xBF</span>));<span class="comment">//转码 防止乱码</span></span><br><span class="line">        fputcsv($fp,$tileArray);</span><br><span class="line">        $index = <span class="number">0</span>;</span><br><span class="line">        foreach ($dataArray <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            <span class="keyword">if</span>($index==<span class="number">1000</span>)&#123;</span><br><span class="line">                $index=<span class="number">0</span>;</span><br><span class="line">                ob_flush();</span><br><span class="line">                flush();</span><br><span class="line">            &#125;</span><br><span class="line">            $index++;</span><br><span class="line">            fputcsv($fp,$item);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        ob_flush();</span><br><span class="line">        flush();</span><br><span class="line">        ob_end_clean();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">exportexcel</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          $sqlWhere = session(<span class="string">'H5OPERATIONLOG_TIME_WHERE'</span>);</span><br><span class="line">          $data = M(self::T_TABLE)-&gt;where(<span class="string">'create_time'</span>.$sqlWhere)-&gt;select();</span><br><span class="line">          foreach ($data <span class="keyword">as</span> $key =&gt; $row) &#123;</span><br><span class="line">              $data[$key][<span class="string">'create_time'</span>] = $row[<span class="string">'create_time'</span>].<span class="string">"\t"</span>;<span class="comment">//加"\t"形成文本格式，原样输出</span></span><br><span class="line">          &#125;</span><br><span class="line">         $filename=<span class="string">"XXXXX"</span>;<span class="comment">//导出文件名</span></span><br><span class="line">         $headArr=array(<span class="string">"XXX"</span>,<span class="string">"XXX1"</span>,<span class="string">"XXX2"</span>);<span class="comment">//数据头</span></span><br><span class="line">         exportToCsv($filename,$headArr,$data);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &lt;td&gt;&lt;a href="你的exportexcel访问路径"&gt;&lt;input type="button" value="导出Excel"&gt;&lt;/input&gt;&lt;/a&gt;&lt;/td&gt;</span><br><span class="line">    当您打开一个文本文件、 CSV 文件和文件的前两个字符是大写字母 <span class="string">"I"</span>，<span class="string">"D"</span> 时，会发生此问题。https:<span class="comment">//learnku.com/articles/32164</span></span><br></pre></td></tr></table></figure>
<h3 id="使用双向链表"><a href="#使用双向链表" class="headerlink" title="使用双向链表"></a>使用双向链表</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyList</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/** @var Node $head */</span></span><br><span class="line">    public $head;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** @var Node $tail */</span></span><br><span class="line">    public $tail;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;head = <span class="literal">null</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;tail = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">remove</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">        $prev = $<span class="keyword">this</span>-&gt;head;</span><br><span class="line">        $cur = $prev-&gt;next;</span><br><span class="line">        <span class="keyword">while</span> (!empty($prev) &amp;&amp; !empty($cur)) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($cur-&gt;data == $data) &#123;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;removeHelper($prev, $cur);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            $prev = $prev-&gt;next;</span><br><span class="line">            $cur = $cur-&gt;next;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">removeHelper</span>(<span class="params">$prev, $cur</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($cur == $<span class="keyword">this</span>-&gt;tail) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;tail = $prev;</span><br><span class="line">        &#125;</span><br><span class="line">        $prev-&gt;next = $cur-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">append</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $n = <span class="keyword">new</span> Node($data);</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32281</span></span><br></pre></td></tr></table></figure>
<h3 id="守护进程"><a href="#守护进程" class="headerlink" title="守护进程"></a>守护进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">fork 子进程</span><br><span class="line">父进程退出</span><br><span class="line">设置新的会话</span><br><span class="line">重置文件掩码</span><br><span class="line">关闭标准输入输出</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">daemon</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    $pid = pcntl_fork();</span><br><span class="line">    <span class="comment">// fork 失败</span></span><br><span class="line">    <span class="keyword">if</span> ($pid &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        exit(<span class="string">'fork failed'</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($pid &gt; <span class="number">0</span>) &#123;</span><br><span class="line">       <span class="comment">// 退出父进程</span></span><br><span class="line">        exit(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 设置新的会员</span></span><br><span class="line">    <span class="comment">// setsid 有几个注意点</span></span><br><span class="line">    <span class="comment">// 不能是进程组的组长调用</span></span><br><span class="line">    <span class="comment">// 对于进程组组员调用会产生新的会话和进程组，并成为该进程组的唯一成员，调用的进程将脱离终端</span></span><br><span class="line">    <span class="keyword">if</span> (posix_setsid() &lt; <span class="number">0</span>) &#123;<span class="comment">// 获取进程ID</span></span><br><span class="line"></span><br><span class="line">        exit(<span class="string">'set sid failed'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 重置文件掩码</span></span><br><span class="line">    umask(<span class="number">0</span>);</span><br><span class="line">    <span class="comment">// 切换工作目录</span></span><br><span class="line">    chdir(<span class="string">'/'</span>);</span><br><span class="line">    <span class="comment">// 关闭标准输入输出</span></span><br><span class="line">    fclose(STDIN);</span><br><span class="line">    fclose(STDOUT);</span><br><span class="line">    fclose(STDERR);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">global $stdin, $stdout, $stderr;</span><br><span class="line">$stdin = fopen(<span class="string">'/dev/null'</span>, <span class="string">'r'</span>);</span><br><span class="line">$stdout = fopen(<span class="string">'/www/php/txt.txt'</span>,<span class="string">'wb'</span>);</span><br><span class="line">$stderr = fopen(<span class="string">'/dev/null'</span>, <span class="string">'wb'</span>);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32320</span></span><br></pre></td></tr></table></figure>
<h3 id="如何获得每个父母的-N-个相关模型"><a href="#如何获得每个父母的-N-个相关模型" class="headerlink" title="如何获得每个父母的 N 个相关模型"></a>如何获得每个父母的 N 个相关模型</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/</span></span><br></pre></td></tr></table></figure>
<h3 id="防止-XSS-注入"><a href="#防止-XSS-注入" class="headerlink" title="防止 XSS 注入"></a>防止 XSS 注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体</span><br><span class="line">strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">XSSFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 配置不执行过滤转换参数项，如 env 配置: XSS_EXCEPT=article_contents,html_contents</span></span><br><span class="line">        $param_except_string = config(<span class="string">'const.xss_filter_param_except'</span>);</span><br><span class="line">        $param_except = [];</span><br><span class="line">        <span class="keyword">if</span> (!empty($param_except_string)) &#123;</span><br><span class="line">            $param_except = explode(<span class="string">','</span>, $param_except_string);</span><br><span class="line">        &#125;</span><br><span class="line">        $input = $request-&gt;except($param_except);</span><br><span class="line">        array_walk_recursive($input, <span class="function"><span class="keyword">function</span> (<span class="params">&amp;$input</span>) </span>&#123;</span><br><span class="line">            <span class="comment">// $input = strip_tags($input);  // 清除标签内容</span></span><br><span class="line">            $input = htmlspecialchars($input, ENT_NOQUOTES, <span class="string">'UTF-8'</span>, <span class="literal">false</span>); <span class="comment">// 过滤转换预定符号</span></span><br><span class="line">        &#125;);</span><br><span class="line">        $request-&gt;merge($input);</span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">p</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> htmlspecialchars($value, ENT_QUOTES, <span class="string">'UTF-8'</span>, <span class="literal">false</span>);</span><br><span class="line">&#125;</span><br><span class="line">$nickname = <span class="string">"&lt;img src='x' onerror='alert(0)' /&gt;测试昵称"</span>;</span><br><span class="line">echo p($nickname);</span><br><span class="line">修改 php.ini 配置开启 httpOnly: Session.cookie_httponly = On</span><br><span class="line">开启 httpOnly 后，禁止 JavaScript 脚本直接读取该 Cookie 内容</span><br><span class="line">cookie($name = <span class="literal">null</span>, $value = <span class="literal">null</span>, $minutes = <span class="number">0</span>, $path = <span class="literal">null</span>, $domain = <span class="literal">null</span>, $secure = <span class="literal">false</span>, $httpOnly = <span class="literal">true</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32529</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-使用-Redis-实现分页"><a href="#PHP-使用-Redis-实现分页" class="headerlink" title="PHP 使用 Redis 实现分页"></a>PHP 使用 Redis 实现分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line">namespace app\Constants\Nomal;</span><br><span class="line">use Illuminate\Support\Facades\Cache;</span><br><span class="line">use Illuminate\Support\Facades\Redis;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RedisList</span></span>&#123;</span><br><span class="line">   <span class="comment">// 添加一条数据到redis中</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zAdd</span>(<span class="params">$key,array $value</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::zadd($key,$value);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 从Redis返回一条数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zRange</span>(<span class="params">$key,$start,$end,$withScores</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>  Redis::zRange($key,$start,$end,$withScores);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Redis 返回一条数据从大小返回数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zrevRange</span> (<span class="params">$key,$start,$end,$withScores</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>  Redis::ZREVRANGE($key,$start,$end,$withScores);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Redis 返回某个数据 按分数正序排序 //ZRANGEBYSCORE</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span>  <span class="title">zRangByScore</span>(<span class="params">$key,$start,$end,$withScores</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::ZRANGEBYSCORE($key,$start,$end,$withScores);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Redis 返回数据按分数倒叙排序 // ZREVRANGEBYSCORE</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zRevRangBySore</span>(<span class="params">$key,$start,$end,$withScores</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::ZREVRANGEBYSCORE($key,$start,$end,$withScores);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 删除某个成员的值</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zRem</span>(<span class="params">$key,$member</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::zRem($key,$member);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 返回zset集合中所有元素的个数</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zSize</span>(<span class="params">$key</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Redis::zcard($key);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 返回比元素$member分数大的元素的个数</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zRevRank</span>(<span class="params">$key,$member</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Redis::zRevRank($key,$member);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果在名称为key的zset中已经存在元素member，则该元素的score增加increment；否则向集合中添加该元素，其score的值为increment</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zIncrBy</span>(<span class="params">$key,$increment,$member</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Redis::zIncrBy($key,$increment,$member);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 求有序集合的并集</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zUnion</span>(<span class="params">$output,$zsetkey</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Redis::zUnion($output,$zsetkey);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//求有序集合的交集</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">zInter</span>(<span class="params">$output,$zsetkey</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Redis::zInter($output,$zsetkey);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// redis 分页处理</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">limitAndPage</span>(<span class="params">$pageIndex,$pageNum</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> array(<span class="string">'pageIndex'</span>=&gt;$pageIndex<span class="number">-1</span>,<span class="string">'pageNumber'</span>=&gt;$pageIndex*$pageNum<span class="number">-1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// redis 队列操作 入队列</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">lPush</span>(<span class="params">$key,$value</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::LPUSH($key,$value);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// redis 根据索引取数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">lIndex</span>(<span class="params">$key,$position</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::lIndex($key,$position);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// redis list维护一个长度为100000的队列</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">lTrim</span>(<span class="params">$key,$start=<span class="number">0</span>,$stop=<span class="number">100000</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::lTrim($key,$start,$stop);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 设置key的过期时间 现在设置的是一个月的时间 做二级缓存使用</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">expire</span>(<span class="params">$key,$timesec =<span class="number">0</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($timesec == <span class="number">0</span>)&#123;</span><br><span class="line">            $expire_time = <span class="number">0</span>&lt;$timesec? $timesec: strtotime(date(<span class="string">'Y-m-d'</span>)) + <span class="number">2592000</span> - time();</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            $expire_time = $timesec;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Redis::expire($key,$expire_time);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 设置zset的长度</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">SetZsetTime</span>(<span class="params">$key,$data,$lenth =<span class="number">4</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;zSize($key)&gt;$lenth)&#123;</span><br><span class="line">            Redis::zremrangebyrank($key,<span class="number">0</span>,count($data)<span class="number">-1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getListInfoByCondition</span>(<span class="params">$key,$start,$end,array $limit,$sort = <span class="string">'asc'</span></span>)</span>&#123;</span><br><span class="line">       <span class="keyword">if</span>($sort == <span class="string">'desc'</span>)&#123;</span><br><span class="line">          $pageNum = $limit[<span class="string">'limit'</span>][<span class="number">0</span>]*$limit[<span class="string">'limit'</span>][<span class="number">1</span>];</span><br><span class="line">          $limit[<span class="string">'limit'</span>][<span class="number">0</span>] = $<span class="keyword">this</span>-&gt;zSize($key)-$pageNum;</span><br><span class="line">          <span class="comment">// 本方法只使用于app分页 上一页 下一页 不支持跳页查询</span></span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> $sort == <span class="string">'desc'</span>?array_reverse($<span class="keyword">this</span>-&gt;zRangByScore($key,$start,$end,$limit)):$<span class="keyword">this</span>-&gt;zRangByScore($key,$start,$end,$limit);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 添加一条数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">addOneInfo</span>(<span class="params">$key,array $data</span>)</span>&#123;</span><br><span class="line">       <span class="keyword">if</span>(Redis::exists($key))&#123;</span><br><span class="line">           <span class="keyword">if</span>(count($data)&gt;<span class="number">4</span>)&#123;</span><br><span class="line">               <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">           &#125;</span><br><span class="line">           $ret = $<span class="keyword">this</span>-&gt;zAdd($key,$data);</span><br><span class="line">           $<span class="keyword">this</span>-&gt;SetZsetTime($key,$data);</span><br><span class="line">           <span class="keyword">return</span> $ret;</span><br><span class="line">       &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">          $<span class="keyword">this</span>-&gt;expire($key,<span class="number">60</span>);</span><br><span class="line">          <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;zAdd($key,$data);</span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 更新一条数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">updateOneById</span>(<span class="params">$key,$id,$data</span>)</span>&#123;</span><br><span class="line">       $<span class="keyword">this</span>-&gt;deleteOneById($key,$id);</span><br><span class="line">       <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;zAdd($key,$data);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 删除一条数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">deleteOneById</span>(<span class="params">$key,$id</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Redis::zremrangebyscore($key,$id,$id);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 获取一条数据</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getOneInfoById</span>(<span class="params">$key,$id</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;zRangByScore($key,$id,$id,array(<span class="string">'withScores'</span>=&gt;<span class="string">''</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https:<span class="comment">//learnku.com/articles/32548</span></span><br></pre></td></tr></table></figure>
<h3 id="繁体简体互转"><a href="#繁体简体互转" class="headerlink" title="繁体简体互转"></a>繁体简体互转</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/BYVoid/OpenCC</span></span><br><span class="line">composer <span class="built_in">require</span> sleep-cat/opencc-php</span><br><span class="line">use SleepCat\OpenCC\Command;</span><br><span class="line">use SleepCat\OpenCC\OpenCC;</span><br><span class="line">$command = <span class="keyword">new</span> Command(<span class="string">'/usr/bin/opencc'</span>,<span class="string">'/usr/share/opencc'</span>);</span><br><span class="line">$opencc = <span class="keyword">new</span> OpenCC($command);</span><br><span class="line"><span class="comment">// laravel应用可用外观</span></span><br><span class="line">$result = OpenCC::transform(<span class="string">'天氣乍涼人寂寞，光陰須得酒消磨。且來花裏聽笙歌。'</span>,<span class="string">'t2s.json'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 其他应用使用实例</span></span><br><span class="line">$result = $opencc-&gt;transform(<span class="string">'天氣乍涼人寂寞，光陰須得酒消磨。且來花裏聽笙歌。'</span>,<span class="string">'t2s.json'</span>);</span><br><span class="line"></span><br><span class="line">print_r($result);</span><br><span class="line"><span class="comment">// 天气乍凉人寂寞，光阴须得酒消磨。且来花里听笙歌。</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/32497</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/26282" target="_blank" rel="noopener">接入 paypal PHP-sdk 支付 / 回调 / 退款全流程</a></p>
<p><a href="https://learnku.com/articles/32019" target="_blank" rel="noopener">PHP 三年模拟五年面试</a></p>
<p><a href="https://learnku.com/articles/32113#reply102242" target="_blank" rel="noopener">phpredis 扩展redis使用</a></p>
<p><a href="https://learnku.com/articles/31844" target="_blank" rel="noopener">数据库主键 ID 生成策略</a></p>
<p><a href="https://learnku.com/articles/31809" target="_blank" rel="noopener">PHP 生成迷宫路线</a></p>
<p><a href="https://learnku.com/laravel/t/31885" target="_blank" rel="noopener"> JavaScript 混淆表达式https://jscrew.it/</a></p>
<p><a href="https://learnku.com/articles/31809" target="_blank" rel="noopener">PHP 生成迷宫路线</a></p>
<p><a href="https://www.bilibili.com/video/av57659470/" target="_blank" rel="noopener">PHP 手册入门视频教程</a></p>
<p><a href="https://learnku.com/articles/31789" target="_blank" rel="noopener"> phpstorm 下安装 Laravel-ide-helper</a></p>
<p><a href="https://learnku.com/laravel/t/31755" target="_blank" rel="noopener">个人收款支付接口</a></p>
<p><a href="https://learnku.com/articles/31748" target="_blank" rel="noopener">数据结构复杂图形存储 PHP 版</a></p>
<p><a href="https://p0sec.net/index.php/archives/107/" target="_blank" rel="noopener">第十届全国大学生信息安全竞赛Web WriteUp</a></p>
<p><a href="https://www.playpi.org/2019042701.html" target="_blank" rel="noopener">解决微博图床防盗链的问题</a></p>
<p><a href="https://www.helingfeng.com/2018-06-13/publish-php-projects-with-jenkins-automation/" target="_blank" rel="noopener">Jenkins 自动化发布 PHP 项目</a></p>
<p><a href="https://p0sec.net/index.php/archives/117/" target="_blank" rel="noopener">SQL注入备忘手册</a></p>
<p><a href="https://learnku.com/articles/28772" target="_blank" rel="noopener">PHP 面试详解之技术篇</a></p>
<p><a href="https://p0sec.net/" target="_blank" rel="noopener">PHP安全</a></p>
<p><a href="https://github.com/aizuyan/pinyin" target="_blank" rel="noopener">将汉字转化为拼音</a></p>
<p><a href="https://www.cnblogs.com/iforever/p/4254605.html" target="_blank" rel="noopener">pack、unpack自制二进制“数据库”</a></p>
<p><a href="https://github.com/xiaohuilam/laravel-wxapp-notification-channel" target="_blank" rel="noopener"> 小程序模板消息</a></p>
<p><a href="https://learnku.com/laravel/t/31373" target="_blank" rel="noopener">Workerman 作为 Laravel 队列进程守护</a></p>
<p><a href="https://learnku.com/articles/31364" target="_blank" rel="noopener">Laravel-Horizon【队列监控】</a></p>
<p><a href="https://qimajiang.com/2017/06/13/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E7%94%A8-PHP-%E6%8B%BC%E6%8E%A5%E5%BE%AE%E4%BF%A1%E5%A5%BD%E5%8F%8B%E5%A4%B4%E5%83%8F/" target="_blank" rel="noopener"> PHP 拼接微信好友头像https://github.com/moxun33/imageMergerUtil/blob/master/MergeImage.class.php</a></p>
<p><a href="https://itzxf.com/index/article/page/id/111.html" target="_blank" rel="noopener">PHP 优秀资源库整理</a></p>
<p><a href="https://github.com/sdfsky/tipask" target="_blank" rel="noopener"> PHP 问答系统</a></p>
<p><a href="https://learnku.com/articles/31145" target="_blank" rel="noopener">Laravel 中使用 极光推送jpush composer包</a></p>
<p><a href="https://www.qingwei.tech/programe-develops/php/622.html" target="_blank" rel="noopener">PHP常用函数整理</a></p>
<p><a href="https://github.com/alicfeng/note.samego.com" target="_blank" rel="noopener">笔记分享 | 简书2GitHub</a></p>
<p><a href="https://learnku.com/articles/31102" target="_blank" rel="noopener"> PHP 中文简易分词</a></p>
<p><a href="https://michael-lik.github.io/php-the-right-way/pages/The-Basics.html" target="_blank" rel="noopener">PHP the right way</a></p>
<p><a href="https://www.fanhaobai.com/2018/09/process-php-multiprocess-server.html" target="_blank" rel="noopener">用PHP玩转进程之二 — 多进程PHPServer</a></p>
<p><a href="https://learnku.com/articles/30763" target="_blank" rel="noopener">后端来配置 Vue Router 的方案</a></p>
<p><a href="https://github.com/BruceDone/awesome-crawler" target="_blank" rel="noopener">爬虫工具</a></p>
<p><a href="https://github.com/owner888/phpspider" target="_blank" rel="noopener">我用php爬虫一天时间“偷了”知乎一百万用户</a></p>
<p><a href="https://qimajiang.com/2017/04/04/%E4%BD%BF%E7%94%A8-ngrok-%E5%AE%9E%E7%8E%B0%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/" target="_blank" rel="noopener"> ngrok 实现内网穿透ittun.com/</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/php/" rel="tag"># php</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/09/命令行录制工具-asciinema/" rel="next" title="命令行录制工具 asciinema">
                <i class="fa fa-chevron-left"></i> 命令行录制工具 asciinema
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/16/如何评价张居正/" rel="prev" title="如何评价张居正">
                如何评价张居正 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#S-O-I-L-D-之单一职责"><span class="nav-number">1.</span> <span class="nav-text">S.O.I.L.D 之单一职责</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字节转换"><span class="nav-number">2.</span> <span class="nav-text">字节转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php多线程"><span class="nav-number">3.</span> <span class="nav-text">php多线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#大文件传输解决方案"><span class="nav-number">4.</span> <span class="nav-text">大文件传输解决方案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-生成奖状"><span class="nav-number">5.</span> <span class="nav-text">PHP 生成奖状</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-install-报错"><span class="nav-number">6.</span> <span class="nav-text">Composer install 报错</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-FPM-与-Nginx-的通信机制总结"><span class="nav-number">7.</span> <span class="nav-text">PHP-FPM 与 Nginx 的通信机制总结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-报错：PHP-Fatal-error-Uncaught-ReflectionException-Class-request-does-not-exist"><span class="nav-number">8.</span> <span class="nav-text">Laravel 报错：PHP Fatal error: Uncaught ReflectionException: Class request does not exist</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSON-响应出现浮点小数溢出"><span class="nav-number">9.</span> <span class="nav-text">JSON 响应出现浮点小数溢出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#原码、反码、补码"><span class="nav-number">10.</span> <span class="nav-text">原码、反码、补码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#APP-KEY"><span class="nav-number">11.</span> <span class="nav-text">APP_KEY</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-识别相片"><span class="nav-number">12.</span> <span class="nav-text">PHP 识别相片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-镜像限额，出现要求用户名认证"><span class="nav-number">13.</span> <span class="nav-text">Composer 镜像限额，出现要求用户名认证</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#AB-压测"><span class="nav-number">14.</span> <span class="nav-text">AB 压测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php爬虫框架Goutte"><span class="nav-number">15.</span> <span class="nav-text">php爬虫框架Goutte</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#防恶意频繁提交"><span class="nav-number">16.</span> <span class="nav-text">防恶意频繁提交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Goutte爬取-IP-代理池"><span class="nav-number">17.</span> <span class="nav-text">Goutte爬取 IP 代理池</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#微软小冰接口的颜值检测"><span class="nav-number">18.</span> <span class="nav-text">微软小冰接口的颜值检测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-fpm-max-children-优化"><span class="nav-number">19.</span> <span class="nav-text">php-fpm max_children 优化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用多个同一数据库驱动"><span class="nav-number">20.</span> <span class="nav-text">使用多个同一数据库驱动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP的MD5加密与JAVA的MD5加密不一致"><span class="nav-number">21.</span> <span class="nav-text">PHP的MD5加密与JAVA的MD5加密不一致</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生成条形码"><span class="nav-number">22.</span> <span class="nav-text">生成条形码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-输入账号密码"><span class="nav-number">23.</span> <span class="nav-text">Composer 输入账号密码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#foreach-引用"><span class="nav-number">24.</span> <span class="nav-text">foreach 引用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取日期数组"><span class="nav-number">25.</span> <span class="nav-text">获取日期数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#女神QQ号码"><span class="nav-number">26.</span> <span class="nav-text">女神QQ号码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP实现linux命令tail-f"><span class="nav-number">27.</span> <span class="nav-text">PHP实现linux命令tail -f</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Unicode到UTF-8"><span class="nav-number">28.</span> <span class="nav-text">Unicode到UTF-8</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PDO-防止-sql-注入"><span class="nav-number">29.</span> <span class="nav-text">PDO 防止 sql 注入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二进制上传图片"><span class="nav-number">30.</span> <span class="nav-text">二进制上传图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#order-by-进行盲注"><span class="nav-number">31.</span> <span class="nav-text">order by 进行盲注</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gbk宽字节注入"><span class="nav-number">32.</span> <span class="nav-text">gbk宽字节注入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-加速"><span class="nav-number">33.</span> <span class="nav-text">Composer 加速</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件下载乱码"><span class="nav-number">34.</span> <span class="nav-text">文件下载乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#直接使用-mysql-utf8-存储-超过三个字节的-emoji-表情"><span class="nav-number">35.</span> <span class="nav-text">直接使用 mysql utf8 存储 超过三个字节的 emoji 表情</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二分查找"><span class="nav-number">36.</span> <span class="nav-text">二分查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限分类"><span class="nav-number">37.</span> <span class="nav-text">无限分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php加密"><span class="nav-number">38.</span> <span class="nav-text">php加密</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文字生成点阵图"><span class="nav-number">39.</span> <span class="nav-text">文字生成点阵图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Guzzle-中间件进行优雅的请求重试"><span class="nav-number">40.</span> <span class="nav-text">Guzzle 中间件进行优雅的请求重试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二进制运算符按位与-amp"><span class="nav-number">41.</span> <span class="nav-text">二进制运算符按位与 &amp;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Composer-安装插件swap-内存不足"><span class="nav-number">42.</span> <span class="nav-text">Composer 安装插件swap 内存不足</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#crc32-检错数据"><span class="nav-number">43.</span> <span class="nav-text">crc32 检错数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#下载带有换行符的文件内容"><span class="nav-number">44.</span> <span class="nav-text">下载带有换行符的文件内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#两个字符串的相似度"><span class="nav-number">45.</span> <span class="nav-text">两个字符串的相似度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#两个文件是否相同"><span class="nav-number">46.</span> <span class="nav-text">两个文件是否相同</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#大转盘红包概率"><span class="nav-number">47.</span> <span class="nav-text">大转盘红包概率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现http协议"><span class="nav-number">48.</span> <span class="nav-text">实现http协议</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#排名算法支持重复排名"><span class="nav-number">49.</span> <span class="nav-text">排名算法支持重复排名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#new-static-和-new-self"><span class="nav-number">50.</span> <span class="nav-text">new static 和 new self</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-导出-PDF"><span class="nav-number">51.</span> <span class="nav-text">PHP 导出 PDF</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strtotime-的困惑"><span class="nav-number">52.</span> <span class="nav-text">strtotime 的困惑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#正则匹配emoji"><span class="nav-number">53.</span> <span class="nav-text">正则匹配emoji</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#合并一个或多个数组"><span class="nav-number">54.</span> <span class="nav-text">合并一个或多个数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#存储货币"><span class="nav-number">55.</span> <span class="nav-text">存储货币</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL-Blob-类型"><span class="nav-number">56.</span> <span class="nav-text">MySQL Blob 类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#中文分割"><span class="nav-number">57.</span> <span class="nav-text">中文分割</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件大小"><span class="nav-number">58.</span> <span class="nav-text">文件大小</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#时间差"><span class="nav-number">59.</span> <span class="nav-text">时间差</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组分组"><span class="nav-number">60.</span> <span class="nav-text">数组分组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#日期获取星期"><span class="nav-number">61.</span> <span class="nav-text">日期获取星期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-距离排序"><span class="nav-number">62.</span> <span class="nav-text">Laravel 距离排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二维数组去重"><span class="nav-number">63.</span> <span class="nav-text">二维数组去重</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#排序"><span class="nav-number">64.</span> <span class="nav-text">排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串转换成数组"><span class="nav-number">65.</span> <span class="nav-text">字符串转换成数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建图片上传目录"><span class="nav-number">66.</span> <span class="nav-text">创建图片上传目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除目录"><span class="nav-number">67.</span> <span class="nav-text">删除目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#xml-转为-array"><span class="nav-number">68.</span> <span class="nav-text">xml 转为 array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归重组"><span class="nav-number">69.</span> <span class="nav-text">递归重组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取文件信息"><span class="nav-number">70.</span> <span class="nav-text">获取文件信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#非递归实现查询该目录下所有文件"><span class="nav-number">71.</span> <span class="nav-text">非递归实现查询该目录下所有文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#单例cron"><span class="nav-number">72.</span> <span class="nav-text">单例cron</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#每三位加个逗号分割"><span class="nav-number">73.</span> <span class="nav-text">每三位加个逗号分割</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Fibonacci数列"><span class="nav-number">74.</span> <span class="nav-text">Fibonacci数列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#验证日期非HTML字符"><span class="nav-number">75.</span> <span class="nav-text">验证日期非HTML字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ip限制"><span class="nav-number">76.</span> <span class="nav-text">ip限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-导出-Excel"><span class="nav-number">77.</span> <span class="nav-text">PHP 导出 Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用双向链表"><span class="nav-number">78.</span> <span class="nav-text">使用双向链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#守护进程"><span class="nav-number">79.</span> <span class="nav-text">守护进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何获得每个父母的-N-个相关模型"><span class="nav-number">80.</span> <span class="nav-text">如何获得每个父母的 N 个相关模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#防止-XSS-注入"><span class="nav-number">81.</span> <span class="nav-text">防止 XSS 注入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-使用-Redis-实现分页"><span class="nav-number">82.</span> <span class="nav-text">PHP 使用 Redis 实现分页</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#繁体简体互转"><span class="nav-number">83.</span> <span class="nav-text">繁体简体互转</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
